Shell Scripting with Bash

执行date命令并输出所有参数到 notes.txt

1
echo $(date): $* >> ~/notes.txt

变量

赋予两个变量的值

1
usergreeting="$greeting, $user"

使用变量用花括号

1
2
3
# get the topic
topic=$1
echo "text" >> ~/${topic}notes.txt

从终端读入数据

1
2
# Ask user for input
read -p "Your note: " note

特殊变量

$#脚本的参数数量

获得一个变量的长度

${#var}

调试模式

全局调试,第一行加上 -x即可

1
2
#!bing/bash -x
........

设置某个段落的Debug,再前后加上set -x , set +x

1
2
3
4
5
6
7
# get the topic
topic="$1"
set -x
# filename to write to
filename="${HOME}/${topic}notes.txt"
set +x
# Ask user for input

条件语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# check number of arguments
if [[ $# -ne 2 ]]; then
echo "Need exactly two arguments"
exit 1
fi
# Nested if
# Which one has most files?
if [[ $length_1 -gt $length_2 ]]; then
echo "$1 is longest"
elif [[ $length_1 -eq $length_2 ]]; then
echo "length is equal"
else
echo "$2 is longest"
fi
if mkdir a; then echo "ok"; else echo "error"; fi
if [[ $note ]]; then
echo "$date: $note" >> "$filename"
echo "Note '$note' saved to $filename"
else
echo "No input; note wasn't saved."
fi
# Is there an argument?
if [[ ! $1 ]]; then
echo "Missing argument"
exit 1
fi
# Check the $filename exists
if [[ =e $filename ]]; then
echo "File ${filename} alreday exists"
exit 1
fi
# Check bin directory exists
if [[ ! -d $bindir ]]; then
# if not: create bin directory
if mkdir "$bindir"; then
echo "create ${bindir}"
else
echo "Could not create ${bindir}."
exit 1
fi
fi
if type "$scriptname"; then
echo "There is already a command with name ${scriptname}"
exit 1
fi

判断

[[ Expression ]]

[[ $str ]] 如果真,str不为空
[[ $str = “something” ]] 如果真,str等于字符串”something”
[[ $str=”something” ]] 永远返回真!注意等号前后少了空格.
[[ -e $filename ]] 如果真,文件$filename存在
[[ -d $dirname ]] 如果真,$dirname是一个目录

算数对比

[[ arg1 OP arg2 ]]

-eq:等于
-ne:不等于
-lt:小于
-gt:大于

两个Expression帮助

1
2
help [[
help test

And,Or,Not

1
2
3
4
5
6
# and
[[ $# -eq 1 && $1="foo" ]]
# or
[[ $a || $b ]]
# not
[[ ! -e $file ]]

输入输出

printf

printf要比echo更强大

1
2
3
4
5
printf "hello %s, how are you?" $USER
printf "p%st\n" a e i o u
printf "%ss home is in %s" $USER $HOME
printf "|%20s |%20s |%20s |\n" $(ls)
printf -v greeting "hello %s, how are you?\n" $USER

input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
echo -n "Are you sure (Y/N)? "
answered=
while [[ ! $answered ]]; do
read -r -n 1 -s answer
if [[ $answer = [Yy] ]]; then
answered="yes"
elif [[ $answer = [Nn] ]]; then
answered="no"
fi
done
printf "\n%s\n" $answered

指定分割符

IFS=: read a b

标准流

0:Standard Input (stdin) /dev/stdin
1:Standard Output (stdout) /dev/stdout
2:Standard Error (stderr) /dev/stderr

"cmd 2> /dev/null" discards all errors
>&2 sends output to stderr (equivalent to 1>&2)
2>&1 redirects stderr into stdout
"cmd > logfile 2 > &1" Sending both error and output to a single file

/dev/null

1
2
3
4
5
6
7
8
9
10
11
if type "$scriptname" > /dev/null 2>&1; then
echo "There is already a command with name ${scriptname}"
exit 1
fi
if [[ $note ]]; then
echo "$date: $note" >> "$filename"
echo "Note '$note' saved to $filename"
else
echo "No input; note wasn't saved." 1>&2
fi
打赏支持:支付宝/微信,感谢赏口饭吃