sh01.sh 标准script模版样式
#!/bin/bash #Program: # This program shows "Hello World!" in your screen. #History: #2005/08/23 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo -e "Hello World! \a \n" exit 0sh02.sh read输入流
#!/bin/bash # Program: # User inputs his first name and last name. Program shows his full name. # History: # 2005/08/23 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input your first name: " firstname # 提示使用者输入 read -p "Please input your last name: " lastname # 提示使用者输入 echo -e "\nYour full name is: $firstname $lastname" # 结果由萤幕输出
sh03.sh date file 创建时间文件
#!/bin/bash # Program: # Program creates three files, which named by user's input # and date command. # History: # 2005/08/23 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # 1. 让使用者输入文件名称,并取得 fileuser 这个变量; echo -e "I will use 'touch' command to create 3 files." # 纯粹显示资讯 read -p "Please input your filename: " fileuser # 提示使用者输入 # 2. 为了避免使用者随意按 Enter ,利用变量功能分析档名是否有配置? filename=${fileuser:-"filename"} # 开始判断有否配置档名 # 3. 开始利用 date 命令来取得所需要的档名了; date1=$(date --date='2 days ago' +%Y%m%d) # 前两天的日期 date2=$(date --date='1 days ago' +%Y%m%d) # 前一天的日期 date3=$(date +%Y%m%d) # 今天的日期 file1=${filename}${date1} # 底下三行在配置档名 file2=${filename}${date2} file3=${filename}${date3} # 4. 将档名创建吧! touch "$file1" # 底下三行在创建文件 touch "$file2" touch "$file3"
sh04.sh 四则运算 简易计算器
#!/bin/bash # Program: # User inputs 2 integer numbers; program will cross these two numbers. # History: # 2005/08/23 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo -e "You SHOULD input 2 numbers, I will cross them! \n" read -p "first number: " firstnu read -p "second number: " secnu total=$(($firstnu*$secnu)) echo -e "\nThe result of $firstnu x $secnu is ==> $total"
script 的运行方式差异
sh xxx.sh 变量不会保留在主线程
source xxx.sh 变量会保留,直到主线程关闭
sh05.sh test表达式运用
测试的标志 代表意义 1. 关於某个档名的『文件类型』判断,如 test -e filename 表示存在否 -e 该『档名』是否存在?(常用) -f 该『档名』是否存在且为文件(file)?(常用) -d 该『档名』是否存在且为目录(directory)?(常用) -b 该『档名』是否存在且为一个 block device 装置? -c 该『档名』是否存在且为一个 character device 装置? -S 该『档名』是否存在且为一个 Socket 文件? -p 该『档名』是否存在且为一个 FIFO (pipe) 文件? -L 该『档名』是否存在且为一个连结档? 2. 关於文件的权限侦测,如 test -r filename 表示可读否 (但 root 权限常有例外) -r 侦测该档名是否存在且具有『可读』的权限? -w 侦测该档名是否存在且具有『可写』的权限? -x 侦测该档名是否存在且具有『可运行』的权限? -u 侦测该档名是否存在且具有『SUID』的属性? -g 侦测该档名是否存在且具有『SGID』的属性? -k 侦测该档名是否存在且具有『Sticky bit』的属性? -s 侦测该档名是否存在且为『非空白文件』? 3. 两个文件之间的比较,如: test file1 -nt file2 -nt (newer than)判断 file1 是否比 file2 新 -ot (older than)判断 file1 是否比 file2 旧 -ef 判断 file1 与 file2 是否为同一文件,可用在判断 hard link 的判定上。 主要意义在判定,两个文件是否均指向同一个 inode 哩! 4. 关於两个整数之间的判定,例如 test n1 -eq n2 -eq 两数值相等 (equal) -ne 两数值不等 (not equal) -gt n1 大於 n2 (greater than) -lt n1 小於 n2 (less than) -ge n1 大於等於 n2 (greater than or equal) -le n1 小於等於 n2 (less than or equal) 5. 判定字串的数据 test -z string 判定字串是否为 0 ?若 string 为空字串,则为 true test -n string 判定字串是否非为 0 ?若 string 为空字串,则为 false。 注: -n 亦可省略 test str1 = str2 判定 str1 是否等於 str2 ,若相等,则回传 true test str1 != str2 判定 str1 是否不等於 str2 ,若相等,则回传 false 6. 多重条件判定,例如: test -r filename -a -x filename -a (and)两状况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 与 x 权限时,才回传 true。 -o (or)两状况任何一个成立!例如 test -r file -o -x file,则 file 具有 r 或 x 权限时,就可回传 true。 ! 反相状态,如 test ! -x file ,当 file 不具有 x 时,回传 true
#!/bin/bash # Program: # User input a filename, program will check the flowing: # 1.) exist? 2.) file/directory? 3.) file permissions # History: # 2005/08/25 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # 1. 让使用者输入档名,并且判断使用者是否真的有输入字串? echo -e "Please input a filename, I will check the filename's type and \ permission. \n\n" read -p "Input a filename : " filename test -z $filename && echo "You MUST input a filename." && exit 0 # 2. 判断文件是否存在?若不存在则显示信息并结束脚本 test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0 # 3. 开始判断文件类型与属性 test -f $filename && filetype="regulare file" test -d $filename && filetype="directory" test -r $filename && perm="readable" test -w $filename && perm="$perm writable" test -x $filename && perm="$perm executable" # 4. 开始输出资讯! echo "The filename: $filename is a $filetype" echo "And the permissions are : $perm"
sh06.sh []表达式,ubuntu默认不支持
#!/bin/bash # Program: # This program shows the user's choice # History: # 2005/08/25 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input (Y/N): " yn [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0 [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0 echo "I don't know what your choice is" && exit 0
sh07.sh 默认参数
[root@www scripts]# vi sh07.sh #!/bin/bash # Program: # Program shows the script name, parameters... # History: # 2009/02/17 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo "The script name is ==> $0" echo "Total parameter number is ==> $#" [ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." \ && exit 0 echo "Your whole parameter is ==> '$@'" echo "The 1st parameter ==> $1" echo "The 2nd parameter ==> $2"
[root@www scripts]# sh sh07.sh theone haha quot The script name is ==> sh07.sh <==档名 Total parameter number is ==> 3 <==果然有三个参数 Your whole parameter is ==> 'theone haha quot' <==参数的内容全部 The 1st parameter ==> theone <==第一个参数 The 2nd parameter ==> haha <==第二个参数sh08.sh shift造成参数变量号码偏移
#!/bin/bash # Program: # Program shows the effect of shift function. # History: # 2009/02/17 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo "Total parameter number is ==> $#" echo "Your whole parameter is ==> '$@'" shift # 进行第一次『一个变量的 shift 』 echo "Total parameter number is ==> $#" echo "Your whole parameter is ==> '$@'" shift 3 # 进行第二次『三个变量的 shift 』 echo "Total parameter number is ==> $#" echo "Your whole parameter is ==> '$@'"
sh06-02.sh 单层if判断
#!/bin/bash # Program: # This program shows the user's choice # History: # 2005/08/25 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input (Y/N): " yn if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then echo "OK, continue" exit 0 fi if [ "$yn" == "N" ] || [ "$yn" == "n" ]; then echo "Oh, interrupt!" exit 0 fi echo "I don't know what your choice is" && exit 0
sh06-03.sh 双层if判断
#!/bin/bash # Program: # This program shows the user's choice # History: # 2005/08/25 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input (Y/N): " yn if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then echo "OK, continue" elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then echo "Oh, interrupt!" else echo "I don't know what your choice is" fi
sh10.sh if判断grep筛选综合例子
#!/bin/bash #Program: # Using netstat and grep to detect WWW,SSH,FTP and Mail services. #History: # 2005/08/28 VBird First release PATH=/bin:/sbin:/use/bin:/usr/sbin:/usr/local/bin:/usr/local:/sbin:~/bin export PATH #1. 先做一些告知动作 echo "Now, i will detect your Linux server's services!" echo -e "The www, ftp, ssh, and mail will be detect! \n" #2. 开始进行一些测试的工作,并且也输出一些资讯 testing=$(netstat -tuln | grep ":80 ") # 侦测看 port 80 在否? if [ "$testing" != "" ]; then echo "WWW is running in your system." fi testing=$(netstat -tuln | grep ":22 ") # 侦测看 port 22 在否? if [ "$testing" != "" ]; then echo "SSH is running in your system." fi testing=$(netstat -tuln | grep ":21 ") # 侦测看 port 21 在否? if [ "$testing" != "" ]; then echo "FTP is running in your system." fi testing=$(netstat -tuln | grep ":25 ") # 侦测看 port 25 在否? if [ "$testing" != "" ]; then echo "Mail is running in your system." fi
sh09-2.sh case in条件选择
#!/bin/bash # Program: # Show "Hello" from $1.... by using case .... esac # History: # 2005/08/29 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH case $1 in "hello") echo "Hello, how are you ?" ;; "") echo "You MUST input parameters, ex> {$0 someword}" ;; *) # 其实就相当於万用字节,0~无穷多个任意字节之意! echo "Usage $0 {hello}" ;; esac
sh12-2.sh function方法函数功能
#!/bin/bash # Program: # Use function to repeat information. # History: # 2005/08/29 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH function printit(){ echo -n "Your choice is " # 加上 -n 可以不断行继续在同一行显示 } echo "This program will print your selection !" case $1 in "one") printit; echo $1 | tr 'a-z' 'A-Z' # 将参数做大小写转换! ;; "two") printit; echo $1 | tr 'a-z' 'A-Z' ;; "three") printit; echo $1 | tr 'a-z' 'A-Z' ;; *) echo "Usage $0 {one|two|three}" ;; esac
可以多看看vim /etc/init.d里面的示例模版写法
sh12-3.sh 方法传递参数
#!/bin/bash # Program: # Use function to repeat information. # History: # 2005/08/29 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH function printit(){ echo "Your choice is $1" # 这个 $1 必须要参考底下命令的下达 } echo "This program will print your selection !" case $1 in "one") printit 1 # 请注意, printit 命令后面还有接参数! ;; "two") printit 2 ;; "three") printit 3 ;; *) echo "Usage $0 {one|two|three}" ;; esac
sh13.sh while until循环
while [ condition ] <==中括号内的状态就是判断式 do <==do 是回圈的开始! 程序段落 done 下面与上面意思刚好相反 until [ condition ] do 程序段落 done
#!/bin/bash # Program: # Repeat question until user input correct answer. # History: # 2005/08/29 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH while [ "$yn" != "yes" -a "$yn" != "YES" ] do read -p "Please input yes/YES to stop this program: " yn done echo "OK! you input the correct answer." #!/bin/bash # Program: # Repeat question until user input correct answer. # History: # 2005/08/29 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH until [ "$yn" == "yes" -o "$yn" == "YES" ] do read -p "Please input yes/YES to stop this program: " yn done echo "OK! you input the correct answer."
sh14.sh while循环
#!/bin/bash # Program: # Use loop to calculate "1+2+3+...+100" result. # History: # 2005/08/29 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH s=0 # 这是加总的数值变量 i=0 # 这是累计的数值,亦即是 1, 2, 3.... while [ "$i" != "100" ] do i=$(($i+1)) # 每次 i 都会添加 1 s=$(($s+$i)) # 每次都会加总一次! done echo "The result of '1+2+3+...+100' is ==> $s"
sh15.sh for循环遍历枚举
#!/bin/bash # Program: # Using for .... loop to print 3 animals # History: # 2005/08/29 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH for animal in dog cat elephant do echo "There are ${animal}s.... " done
sh16.sh foreach循环遍历数组
#!/bin/bash # Program # Use id, finger command to check system account's information. # History # 2009/02/18 VBird first release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH users=$(cut -d ':' -f1 /etc/passwd) # 撷取帐号名称 for username in $users # 开始回圈进行! do id $username finger $username done
sh17.sh foreach 循环遍历个数seq
#!/bin/bash # Program # Use ping command to check the network's PC state. # History # 2009/02/18 VBird first release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH network="192.168.1" # 先定义一个网域的前面部分! for sitenu in $(seq 1 100) # seq 为 sequence(连续) 的缩写之意 do # 底下的程序在取得 ping 的回传值是正确的还是失败的! ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1 # 开始显示结果是正确的启动 (UP) 还是错误的没有连通 (DOWN) if [ "$result" == 0 ]; then echo "Server ${network}.${sitenu} is UP." else echo "Server ${network}.${sitenu} is DOWN." fi done
sh18.sh foreach 循环+test
#!/bin/bash # Program: # User input dir name, I find the permission of files. # History: # 2005/08/29 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # 1. 先看看这个目录是否存在啊? read -p "Please input a directory: " dir if [ "$dir" == "" -o ! -d "$dir" ]; then echo "The $dir is NOT exist in your system." exit 1 fi # 2. 开始测试文件罗~ filelist=$(ls $dir) # 列出所有在该目录下的文件名称 for filename in $filelist do perm="" test -r "$dir/$filename" && perm="$perm readable" test -w "$dir/$filename" && perm="$perm writable" test -x "$dir/$filename" && perm="$perm executable" echo "The file $dir/$filename's permission is $perm " done
sh19.sh for do done
#!/bin/bash # Program: # Try do calculate 1+2+....+${your_input} # History: # 2005/08/29 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input a number, I will count for 1+2+...+your_input: " nu s=0 for (( i=1; i<=$nu; i=i+1 )) do s=$(($s+$i)) done echo "The result of '1+2+3+...+$nu' is ==> $s"
debug调试
sh [-nvx] scripts.sh 选项与参数: -n :不要运行 script,仅查询语法的问题; -v :再运行 sccript 前,先将 scripts 的内容输出到萤幕上; -x :将使用到的 script 内容显示到萤幕上,这是很有用的参数!