echo ”历经千辛万苦,我终于爬上来了“
1. Create a multiple line comment:(COMMENT1区间里面的内容为注释)
#!/bin/bash
echo "Adding new users to LDAP Server..."
<<COMMENT1
Master LDAP server : dir1.nixcraft.net.in
Add user to master and it will get sync to backup server too
Profile and active directory hooks are below
COMMENT1
echo "Searching for user..."
2. Enable debug mode: (可以逐行执行,相当于Win下的echo 打开状态)
bash -x test.sh
3. $引用
${SHELL}Code //隔离开SHELL和Code
echo ${i:=0} //如果i没有被定义则定义为1
echo $(date) //date等命名要()起来才被认为是命令
4. 变量定义:以_或者字母开头
5. 变量与表达式:
1. 算术计算:$((expression))
2. declare -i x=10 //指定x为整型变量
3. 定义常量:readonly var=xxx 或者:declare -r var=xxx
4. 检查变量是否存在:$(var:? Error Message) //var=1,第一参数
5. 比较: > < == !=
6. test -f /aa.txt && cat /aa.txt || echo “file not found”
//如果存在/aa.txt显示其内容,不存在的话就报预定义的错误。
// test可以用[]代替,[之后,和]之前要有空格。 test -z $v,检查v是为空
7. Test “$answer” = “yes” //比较字符串,注意=左右各有空格,要用引号。
8. $? 查看上一个命令执行的返回码,0表示成功。
9. CommandA&&CommandB 逻辑或,A执行成功才执行B; A||B,A执行失败执行B.
test $(id -u) -eq 0 && echo "You are root" || echo "NOT root"
10. Logical not: [ ! -d /backup ] && mkdir /backup
11. 大小写转换:echo "TeSt" | tr '[:upper:]' '[:lower:]'
//或者 var="TesT" tr '[:upper:]' '[:lower:]' <<<"$var"
12. 检查是否是root is_user_root(){
[ "$(id -u)" != "0" ] && die "You must be root to run this script" 2}
6. 输入输出:
Read:read -t 10 -p "Enter your name : " name // timeout 10s
Redirection:
1. wc -w <<EOF
> This is a test.
> Apple juice.
> 100% fruit juice and no added sugar, colour or preservative.
> EOF
2. wc -w <<<$(netstat -i | cut -d" " -f1 | egrep -v "^Kernel|Iface|lo")
3. sed: sed ‘/enabled=yes/c\enabled=no’ tftp > tftp.tmp //替换一整行
sed ‘s/GPGCHECK/gpgcheck/’ tftp >tftp.tmp //替换关键字
7. 正则表达式:
^String 匹配以String开头的行
String$ 匹配以String结尾的行
^.$ 只有一个字符的行;
[numbers] 包含任意一个Number; [a-c] 包含任意一个a到c的字母; [^a-c] 不包含
8. 控制语句:
|| if - then语句 ||
if condition
then
Commands
elif condition
then
fi
//goto语句,没有,但是可以通过在执行语句中加入$0来实现调用自己。
|| case语句 ||
case $variable-name in
pattern1|pattern2|pattern3)
command1
;;
pattern4|pattern5|pattern6)
command1
;;
[Ss][Qq][Ll]) //匹配SQL大小写形式,可以用shopt -s nocasematch来从
//整体上定义。 停止shopt -u nocasematch
command1
;;
*)
esac
|| Exit select loop ||
select var in a b c exit //列出1.a 2. b 3.c 让用户选择
do
xxxxxx
Done
//Until loop
#!/bin/bash
i=1
until [ $i -gt 6 ]
do
echo "Welcome $i times."
i=$(( i+1 ))
done
//While loop
While true .OR. :
do
Xxx
Done
//For loop
for ((i=0;i<200;i++)) do userdel user$i;done
File attributes comparisons
-a/-e: True if file exists.
-b: True if file exists and is a block special file.
-c: True if file exists and is a character special file.
-d: True if file exists and is a directory.
-f: True if file exists and is a regular file.
-g: True if file exists and is set-group-id.
-h: True if file exists and is a symbolic link.
-k: True if file exists and its ‘‘sticky’’ bit is set.
-p: True if file exists and is a named pipe (FIFO).
-r: True if file exists and is readable.
-s: True if file exists and has a size greater than zero.
-t: True if file descriptor fd is open and refers to a terminal.
-u: True if file exists and its set-user-id bit is set.
-w: True if file exists and is writable.
-x: True if file exists and is executable.
-O: True if file exists and is owned by the effective user id.
-G: True if file exists and is owned by the effective group id.
-L: True if file exists and is a symbolic link.
-S: True if file exists and is a socket.
-N: True if file exists and has been modified since it was last read.
File attributes comparisons
#!/bin/bash
echo "The script name : $0"
echo "The value of the first argument to the script : $1"
echo "The value of the second argument to the script : $2"
echo "The value of the third argument to the script : $3"
echo "The number of arguments passed to the script : $#"
echo "The value of all command-line arguments (\$* version) : $*"
echo "The value of all command-line arguments (\$@ version) : $@"
//$* 代表原始的参数, $@ 代表加了IFS之后的参数。
9. 常用命令:
kill -9 PID
小试:
//9*9 乘法表
#!/bin/bash
test $# -eq 0 && n=9 || n=$1
for i in {1..9}
do
if test $n -lt $i
then
continue
fi
for j in {1..9}
do
if test $i -lt $j
then
break
fi
printf "%-4d" $(($i * $j))
Done
printf "\n"
Done
//解析文件
#!/bin/bash
file=/etc/passwd
## 以:做分隔附读取/etc/passwd各字段分输出 UID>500的记录
while IFS=':' read -r f1 f2 f3 f4 f5 f6 f7
Do
[ $f3 -ge 500 ] && echo -e "\033[31m UserName: \033[32m $f1, \033[31m Enabled: \033[32m `test "$f2" = "x" && echo yes || echo no`, \033[31m USD: \033[32m $f3,\033[31m GID: \033[32m $f4,\033[31m Description: \033[32m $f5,\033[31m HomeDir:\033[32m $f6,\033[31m StartUpShell:\033[32m $f7"
done < "$file"
echo -e "\033[0m"
while :; do clear & date +%H:%M:%S & sensors & sleep 2; done //查看温度,先装lm-sensors.
10. 方法:
1. 方法的引用:
source /etc/init.d/function &>/dev/null
echo $? //返回的值是source有没有发现文件
2. 处理CTRL+C
trap '' SIGINT SIGQUIT SIGTSTP