最近文章

Linux shell判断字符串是否以某些字符开头

==比较使用bash检查字符串是否以某些字符开头可以使用==比较[[ $str == h* ]]示例str="hello" if [[ $str == h* ]]; then  echo 'yes' fi有两个地方需要注意:h*不需要使用引号括起来,使用引号括起来是直接做相等比较比较语句使
标签:

Nginx禁用ip访问

在Nginx配置禁用ip访问有两种方法:方法一:新增一个server,设置为默认的虚拟主机如下:server { listen 80 default; server_name _; return 403; }方法二、只允许指定域名进入listen 80; server_name www.example.com; if ($host !=
标签:

配置Nginx解决:Incompatibile SockJS! Main site uses: "1.1.5", the iframe: "1.0.0"

前端使用ant-design开发,后端是由很多微服务构成,为了统一访问入口,在前端与后端之间使用了Nginx做代理。前后端直接调用服务是没有报错的,使用Nginx代理后,报sockejs的错误。错误信息:Incompatibile SockJS! Main site uses: "1.1.5", the iframe: "1.0.0"查了下,原因ant-design的热更新是通过websocket
标签:

给多个github账号添加不同的ssh key

1、创建不同的public key$ ssh-keygen -t rsa -C "your_email@example.com"记得替换命令行里的邮件。假如创建两个public key:~/.ssh/id_rsa_jack~/.ssh/id_rsa_tom然后,添加这两个key到ssh$ ssh-add ~/.ssh/id_rsa_jack$ ssh-add ~/.ssh/id_rsa_tom另外
标签:

Linux shell脚本拼接字符串变量

在shell脚本里,使用${var}引用变量,在双引号表示的字符串里,${var}会使用变量的值替换,示例如下:foo="Hello"foo="${foo} World"echo "${foo}"> Hello World多个字符串变量:a='Hello'b='World'c="${a} ${b}"echo "${c}"> Hello World
标签:

shell命令在原文件查找和替换字符串

sed在文件直接替换字符串最简单的是使用sed,如sed -i -e 's/abc/XYZ/g' myfile.txt-i[SUFFIX], --in-place[=SUFFIX]表示就地编辑,如果后面提供了后缀,则会把原文件以提供的后缀做备份。-e script, --expression=script指定执行编辑的脚本,这里使用了替换表达式。表达式格式为s/被替换文本/新的文本/gperl也可
标签:

安装Postman shell脚本(包括.desktop文件)

安装Postman shell脚本install-postman.sh:#!/bin/bash cd /tmp || exit echo "Downloading Postman ..." wget -q https://dl.pstmn.io/download/latest/linux?
标签:

CentOS 7安装pip3

首先要确保在CentOS 7上已经安装了EPEL仓库。如果没有安装,执行以下命令安装:yum install -y epel-release 安装后,安装pip3$sudo yum install python34-pip $pip3.4 install foo
标签:

Ubuntu在终端显示Git的分支名

在Ubuntu的~/.bashrc添加代码,用于在终端显示git的分支名# 显示git分支 force_color_prompt=yes color_prompt=yes parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } if [ "$color_promp
标签:

shell使用IFS设置分隔符分割字符串

Shell脚本分割字符串,可以使用internal field separator (IFS)。IFS=';' read -ra ADDR <<< "$IN" for i in "${ADDR[@]}"; do  &nbs
标签:

Linux shell命令监控文件的变化

1、tail:监控文件的实时更新tail -f logfile.log 2、watch:定时执行命令并输出命令内容,对于有改变的输出会以高亮的方式显示watch -n 10 -d ls -l /var/ -n:设置每隔多少秒执行指定的命令-d:设置执行的命令示例里是每10秒执行ls命令。
标签:

Linux批量删除正则匹配的的文件

1、使用find查找匹配的文件find ./ -regex .*\.gz 注意前面的‘.*’,表示查找到的文件带有目录2、使用xargs 传递文件列表参数给rmfind ./ -regex .*\.gz|xargs rm -rf 使用``代替|xargs 管道传递参数rm -rf 
标签:

Ubuntu安装MySQL去掉密码输入提示

在Ubuntu安装MySQL时,在终端会提示输入root密码。以下介绍如何去掉密码输入提示,静默安装的方式.方法一sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password ${your_password}' sudo
标签:

Linux shell脚本获取命令行的输出

在Linux使用命令替换来获取命令行的输出命令替换格式:$(command) 或者`command` 这里的`为反引号。示例OUTPUT="$(ls -1)" echo "${OUTPUT}" 参考:Bash Reference Manual
标签:

Linux shell判断字符串是否以某些字符开头

==比较

使用bash检查字符串是否以某些字符开头可以使用==比较

[[ $str == h* ]]

示例

str="hello"
if [[ $str == h* ]];
then
 echo 'yes'
fi

有两个地方需要注意:

  1. h*不需要使用引号括起来,使用引号括起来是直接做相等比较
  2. 比较语句使用双中括号括起来,而不是使用单中括号

=~正则比较

如果使用Bash的正则

str="hello"
if [[ "$str" =~ ^he.* ]]; then
    echo "yes"
fi

使用正则匹配字符串的开头字符需要注意:

  • he*:不要使用he*,这里的*号表示e字符0到多个,即h,以及heeee都是测试通过的
  • he.*:这里只允许包含he的字符串通过测试
  • ^he.*:这个表示是以he开头的字符串通过检测

Nginx禁用ip访问

在Nginx配置禁用ip访问有两种方法:

方法一:新增一个server,设置为默认的虚拟主机

如下:

server {
    listen       80 default;
    server_name  _;
    return      403;
}

方法二、只允许指定域名进入

listen       80;
server_name  www.example.com;
if ($host != 'www.example.com'){
   return 403;
}

其中,返回403表示禁止访问的意思

配置Nginx解决:Incompatibile SockJS! Main site uses: "1.1.5", the iframe: "1.0.0"

前端使用ant-design开发,后端是由很多微服务构成,为了统一访问入口,在前端与后端之间使用了Nginx做代理。前后端直接调用服务是没有报错的,使用Nginx代理后,报sockejs的错误。错误信息:

Incompatibile SockJS! Main site uses: "1.1.5", the iframe: "1.0.0"

查了下,原因ant-design的热更新是通过websocket的,nginx需要配置对nginx的支持。

打开nginx配置文件,nginx.conf,在server节点的配置中对websocket的url做拦截,示例配置如下:

server {
listen 80;
server_name example.com;
charset utf-8;
location /websocket/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

关键配置:

  • location /websocket/:配置拦截的websocket路径,根据请求的url前缀调整。
  • proxy_http_version 1.1:websocket是在http1.1上扩展的。
  • proxy_set_header Upgrade $http_upgrade 和proxy_set_header Connection "upgrade":告诉nginx升级协议为websocket

给多个github账号添加不同的ssh key

1、创建不同的public key

$ ssh-keygen -t rsa -C "your_email@example.com"

记得替换命令行里的邮件。

假如创建两个public key:

~/.ssh/id_rsa_jack
~/.ssh/id_rsa_tom

然后,添加这两个key到ssh

$ ssh-add ~/.ssh/id_rsa_jack
$ ssh-add ~/.ssh/id_rsa_tom

另外,清除缓存里的key的命令是:

ssh-add -D

查看key列表的命令是:

$ ssh-add -l

2、修改ssh配置

$ cd ~/.ssh/

$ touch config

$ subl -a config

然后添加key到git:

#first account
Host github.com-jack
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_jack
#second account
Host github.com-tom
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_tom

3、clone git仓库,修改git配置

克隆git仓库,

git clone git@github.com:jack/gfs.git gfs_tom

然后进入gfs_tom,修改git配置:

$ git config user.name "tom"
$ git config user.email "tom@exampl.com"
$ git config user.name "jack"
$ git config user.email "jack@exampl.com"

如果想设为全局的如下:

$ git config --global user.name "tom" 
$ git config --global user.email "tom@example.com"

最后,就可以正常使用git了:

$ git add .
$ git commit -m "提交说明"
$ git push

记得把各个命令的用户信息替换为你自己的。

Linux shell脚本拼接字符串变量

在shell脚本里,使用${var}引用变量,在双引号表示的字符串里,${var}会使用变量的值替换,示例如下:

foo="Hello"
foo="${foo} World"
echo "${foo}"
> Hello World

多个字符串变量:

a='Hello'
b='World'
c="${a} ${b}"
echo "${c}"
> Hello World

shell命令在原文件查找和替换字符串

sed

在文件直接替换字符串最简单的是使用sed,如

sed -i -e 's/abc/XYZ/g' myfile.txt

-i[SUFFIX], --in-place[=SUFFIX]

表示就地编辑,如果后面提供了后缀,则会把原文件以提供的后缀做备份。

-e script, --expression=script

指定执行编辑的脚本,这里使用了替换表达式。表达式格式为

s/被替换文本/新的文本/g

perl

也可以使用perl命令

perl -pi -e 's/abc/XYZ/g' myfile.txt

-i:也是表示就地编辑。

安装Postman shell脚本(包括.desktop文件)

安装Postman shell脚本install-postman.sh:

#!/bin/bash
cd /tmp || exit
echo "Downloading Postman ..."
wget -q https://dl.pstmn.io/download/latest/linux?arch=64 -O postman.tar.gz
tar -xzf postman.tar.gz
rm postman.tar.gz

echo "Installing to opt..."
if [ -d "/opt/Postman" ];then
    sudo rm -rf /opt/Postman
fi
sudo mv Postman /opt/Postman

echo "Creating symbolic link..."
if [ -L "/usr/bin/postman" ];then
    sudo rm -f /usr/bin/postman
fi
sudo ln -s /opt/Postman/Postman /usr/bin/postman

echo "Installation completed successfully."

Postman.desktop文件:

[Desktop Entry]
Encoding=UTF-8
Name=Postman
Exec=postman
Icon=/opt/Postman/resources/app/assets/icon.png
Terminal=false
Type=Application
Categories=Development;

CentOS 7安装pip3

首先要确保在CentOS 7上已经安装了EPEL仓库。如果没有安装,执行以下命令安装:

yum install -y epel-release

安装后,安装pip3

$sudo yum install python34-pip
$pip3.4 install foo

Ubuntu在终端显示Git的分支名

更新于 2018.03.06 1分钟阅读 0 评论 5 推荐

    作者:

在Ubuntu的~/.bashrc添加代码,用于在终端显示git的分支名

# 显示git分支
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt

shell使用IFS设置分隔符分割字符串

Shell脚本分割字符串,可以使用internal field separator (IFS)。

IFS=';' read -ra ADDR <<< "$IN"
for i in "${ADDR[@]}"; do
    # 处理 "$i"
done

单行命令

 while IFS=';' read -ra ADDR; do
      for i in "${ADDR[@]}"; do
          # 处理 "$i"
      done
 done <<< "$IN"

Linux shell命令监控文件的变化

1、tail:监控文件的实时更新

tail -f logfile.log

2、watch:定时执行命令并输出命令内容,对于有改变的输出会以高亮的方式显示

watch -n 10 -d ls -l /var/

-n:设置每隔多少秒执行指定的命令
-d:设置执行的命令

示例里是每10秒执行ls命令。

Linux批量删除正则匹配的的文件

1、使用find查找匹配的文件

find ./ -regex .*\.gz

注意前面的‘.*’,表示查找到的文件带有目录

2、使用xargs 传递文件列表参数给rm

find ./ -regex .*\.gz|xargs rm -rf

使用``代替|xargs 管道传递参数

rm -rf  `find ./ -regex .*\.gz`

此命令等同于上面的命令。

Ubuntu安装MySQL去掉密码输入提示

发布于 2017.10.20 1分钟阅读 0 评论 4 推荐

    Linux

    作者: tocccc
  1. Linux shell脚本获取命令行的输出 Page 1
  2. Ubuntu安装MySQL去掉密码输入提示 Page 2

在Ubuntu安装MySQL时,在终端会提示输入root密码。以下介绍如何去掉密码输入提示,静默安装的方式.

方法一

sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password ${your_password}'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password ${your_password}'
sudo apt-get -y install mysql-server

${your_password}替换为你的密码,也可以留空,这样root密码为空密码。

方法二

使用noninteractive,禁用交互

export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get -q -y install mysql-server

此处,root密码为空密码。

空密码处理

对于空的root密码,可以使用mysqladmin添加root密码:

mysqladmin -u root password ${your_password}

${your_password}替换为你的密码。

Linux shell脚本获取命令行的输出

发布于 2017.10.15 9分钟阅读 0 评论 2 推荐

    Linux

    作者: tocccc
  1. Linux shell脚本获取命令行的输出 Page 1
  2. Ubuntu安装MySQL去掉密码输入提示 Page 2

在Linux使用命令替换来获取命令行的输出

命令替换格式:

$(command)

或者

`command`

这里的`为反引号。

示例

OUTPUT="$(ls -1)"
echo "${OUTPUT}"

参考:Bash Reference Manual