FaiChou

shell

22 Jun 2018

执行shell

./test.sh
sh test.sh
bash test.sh
chmod +x test.sh

基础语法

变量

a=1
echo $a

# parameter substitution
echo ${a}
echo ${a}dollar
echo "$a is 1" # 1 is 1

# just string ...
echo '$a is 1' # $a is 1

# command substitution
echo "I am in $(pwd)" # I am in /Users/FaiChou
echo "I am in `pwd`" # I am in /Users/FaiChou

# in bash all variables are global, if you want to set local variable:
function t() {
  local t=1
}

b=1
export c=2
set | grep 'b=' # b=1 
env | grep 'b=' # no

set | grep 'c=' # no
env | grep 'c=' # c=2

$0 # scriptname or path+scriptname
$1 $2 ... $10 # arguments
$# # arguments counts
$? $ last command output

source

Once the shell script exits, its environment is destroyed. But MYVAR keeps its value of hello within your interactive shell.

In order to receive environment changes back from the script, we must source the script - this effectively runs the script within our own interactive shell, instead of spawning another shell to run it.

We can source a script via the “.” (dot) command.

条件判断

[ $a = 1 ]
[[ $a = 1 ]] # both ok, recommend [[]]

[[ $a = 1 ]]
[[ $a == 1 ]]
[[ $a -eq 1 ]] # both ok, recommend ==

==, !=, >, <, -eq, -ne, -gt, -lt, -ge, -le # no >= and no <=

str=""
[[ $str == "" ]]
[[ -z $str]] # zero, means no define or empty string
[[ -n $str]] # not empty

str="hello"
[[ $str == he* ]] # support RegExp

[[ -e file ]] # file or dir exist
[[ -f file ]] # file exist
[[ -d dir ]]  # dir exist

[[ ! $str == h*llo || 1 == 1 ]] # support && and ||

if [[ exp1 ]]; then
  echo "c1"
elif [[ exp2 ]]; then
  echo "c2"
else
  echo "c3"
fi

if [[ exp ]]; then
  echo "c"
fi

for i in `ls`; do
  echo $i
done

串联命令

pipe

youtube-dl $1 -q -o - | ffmpeg -i - $2 # use pipe(|) send youtube-dl output to ffmpeg

Pipe is used to pass output to another program or utility. Redirect is used to pass output to either a file or stream.

redirect

A > B # 将A的输出保存到B文件

A > TMP && B < TMP #  将A输出保存到TMP文件,再将TMP文件内容当做参数执行B

A | B # 就是上一条命令的等价。


ls exist.sh not_exist.sh 1>success 2>fail # stdout to success, stderr to fail

ls exist.sh not_exist.sh >success 2>&1 # stdout to success, stderr to output

command > /dev/null 2>&1 # ignore output but stderr to output

command &>/dev/null # same as above

command < file # same as cat file | command, but reduce once IO

其他命令

last command

$ mkdir abcdefghijklmn
$ cd !$ # auto complete abc...mn

$ cmd # cmd permission denied
$ sudo !! # expand last command

grep

$ cat file.txt
abcdef
hijklmn

$ grep abc file.txt
abcdef

$ grep -v abc file.txt
hijklmn

$ pip3 list | grep werkzeug -i # ignore case

xargs

echo "a" | xargs rm # do blow things

for arg in read_input; do
    rm arg
done

sed gsed

$ brew install gnu-sed
$ which gsed
# /usr/local/bin/gsed

echo "a b\nc d"
# a b
# c d
echo "a b\nc d" | gsed 's/a/aa/g'
# aa b
# c d

echo "a b\nc d" | gsed -e 's/a/aa/g' -e 's/b/bb/g'

gsed -i '/HEADER_SEARCH_PATHS/c HEADER_SEARCH_PATHS = (\n"$(inherited)",\n"$(SRCROOT)/../../../ios/Pods/Headers/Public/**",\n);' tmp
# replace HEADER_SEARCH_PATHS to blabla..

awk

ifconfig | sed -n -e '/127.0.0.1/d' -e '/inet /p' | awk '{print $2}'

echo "a;b;c" | awk -F';' '{print $2}'

tr

echo 'a,b,c,d' | tr , '\n'
# a
# b
# c
# d

find

find . -iname "*.jpg" # ignore case all .jpg .JPG
find . -type d # find directionaries
find . -type f # find files
find ./path/ -name '*.txt' | xargs grep 'string'

Mac

sudo spctl --master-disable # mac 允许所有来源
defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder
# 展示隐藏
defaults write com.apple.finder AppleShowAllFiles -boolean false ; killall Finder # 隐藏隐藏文件

$ lsof -i:8000 # 查看占有8000端口的进程
$ ps aux | grep QQ # 查看并匹配和QQx相匹的进程

$ du -hs * # list current dir file size
$ du -shc * # list current dir file size and list total size

# command info

➜  ~ which simpleServer
simpleServer: aliased to python -m http.server
➜  ~ which http-server
/usr/local/bin/http-server
➜  ~ command -V simpleServer
simpleServer is an alias for python -m http.server
➜  ~ command -V http-server
http-server is /usr/local/bin/http-server
➜  ~ type simpleServer
simpleServer is an alias for python -m http.server
➜  ~ type -a simpleServer
simpleServer is an alias for python -m http.server
➜  ~ type http-server
http-server is /usr/local/bin/http-server

# open server

simpleServer
http-server
serve

# lookup a command

man command
cht command # curl cht.sh/$1
tldr command

参考链接


comments powered by Disqus