shell 学习笔记
这里主要通过例子来学习。
case
case 值 in
模式 1)
command1
command2
command3
;;
模式 2)
command1
command2
command3
;;
*)
command1
command2
command3
;;
esac
;; 与其他语言中的 break 类似
option="${1}"
case ${option} in
-f) FILE="${2}"
echo "File name is $FILE"
;;
-d) DIR="${2}"
echo "Dir name is $DIR"
;;
*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
;;
esac
for
#!/bin/bash
for k in $( seq 1 10 )
do
mkdir /aaa${k}
done
列出 var 目录下各子目录占用磁盘空间的大小。
DIR="/var"
cd $DIR
for k in $(ls $DIR)
do
[ -d $k ] && du -sh $k
done
while
#!/bin/bash
declare -i i=1
declare -i sum=0
while ((i<=10))
do
let sum+=i
let ++i
done
echo $sum
awk
awk 的强大与复杂让人又爱又恨。
awk [-F field-separator ] ‘commands’ input-file(s)
awk -F = '{if($1~/b_path/) print $2}' /Users/yingdai/.config/zsh-command/path.config
awk -F = '{if($1=="b_path") print $2}' /Users/yingdai/.config/zsh-command/path.config
⇒ '/Users/yingdai/workspace/blog/public_html'
sed
grep
Exit Status
Commands (including the scripts and shell functions we write) issue a value to the system when they terminate, called an exit status. A value of zero indicates success and any other value indicates failure.
test
Expression | Description |
---|---|
-d file | True if file is a directory. |
-e file | True if file exists. |
-f file | True if file exists and is a regular file. |
-L file | True if file is a symbolic link. |
-r file | True if file is a file readable by you. |
-w file | True if file is a file writable by you. |
-x file | True if file is a file executable by you. |
file1 -nt file2 | True if file1 is newer than (according to modification time) file2 |
file1 -ot file2 | True if file1 is older than file2 |
-z string | True if string is empty. |
-n string | True if string is not empty. |
string1 = string2 | True if string1 equals string2. |
string1 != string2 | True if string1 does not equal string2. |
$
echo $? 上一个命令是否执行正确