shell脚本
shell脚本的三种调试方法:
-n 读一遍脚本中的命令但不执行,用来检查脚本中的语法错误
-v 一边执行脚本,一边将执行过的脚本命令打印到标准输出端
-x 提供跟踪执行信息,将执行的每一条命令和结果一次打印出来,这应该就像C语言的gdb一样有用吧,有助于检查错误
使用这些选项有三种方法
1.在命令行提供参数:$sh -x script.sh
2.脚本开头提供参数:#!/bin/sh -x
3.在脚本中用set命令启用or禁用参数:其中set -x表启用,set +x表禁用
另:在写脚本的时候,需特别注意添加空格或换行或分号
eg text命令: $[ 5 -lt 10 ]左方括号后须有空格,右方括号前也须有空格
shell脚本中函数定义: foo() {后须有空格或换行
shell脚本中,若同一行内写有两条命令须加分号
1 交互式获取内容设置到shell变量
#!/bin/bash read -p "Please input your first name: " firstname #提示用户输入 read -p "Please input your last name: " lastname #提示用户输入 echo -e "\nYour full name is: $firstname $lastname, congratulation!"
输出
asn@asn-vm:~/Desktop/scripts$ ./sh02.sh
Please input your first name: asn
Please input your last name: judy
Your full name is: asn judy, congratulation!
2 循环
1) 打印系统中所有用户的信息
#!/bin/bash # Program # Use id, finger command to check system account's information. users=$(cut -d ':' -f1 /etc/passwd) #获取帐号名称 for username in $users do id $username finger $username done
输出:
asn@asn-vm:~/Desktop/scripts$ ./shell-user.sh
uid=0(root) gid=0(root) groups=0(root) Login: root Name: root Directory: /root Shell: /bin/bash Never logged in. No mail. No Plan.
uid=1(daemon) gid=1(daemon) groups=1(daemon) Login: daemon Name: daemon Directory: /usr/sbin Shell: /usr/sbin/nologin Never logged in. No mail. No Plan.
uid=1000(asn) gid=1000(asn) groups=1000(asn),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare),125(docker) Login: asn Name: asnjudy@163.com Directory: /home/asn Shell: /bin/bash On since Fri Jul 10 19:25 (CST) on :0 from :0 (messages off) On since Sat Jul 11 01:16 (CST) on pts/1 from :0 3 seconds idle Last login Fri Jul 10 21:15 (CST) on pts/25 from 192.168.204.1 No mail. No Plan.
uid=116(sshd) gid=65534(nogroup) groups=65534(nogroup) Login: sshd Name: Directory: /var/run/sshd Shell: /usr/sbin/nologin Never logged in. No mail. No Plan.
2)输入一个目录,查看该目录内文件的权限
#!/bin/bash read -p "Please input a directory:" dir if [ "$dir" == "" -o ! -d "$dir" ]; then echo "The $dir is NOT a existing directory in your system." exit 1 fi #开始测试文件 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
输出:
asn@asn-vm:~/Desktop/scripts$ chmod +x shell-dir.sh asn@asn-vm:~/Desktop/scripts$ ./shell-dir.sh Please input a directory:/home/asn/Desktop The file /home/asn/Desktop/alauda-CLI's permission is readable writable executable The file /home/asn/Desktop/c_test's permission is readable writable executable The file /home/asn/Desktop/scripts's permission is readable writable executable The file /home/asn/Desktop/shell-if.sh~'s permission is readable writable executable The file /home/asn/Desktop/test.cpp's permission is readable writable
3 函数
#!/bin/bash printit() { echo "Your choice is $1" } echo "This program will print your selection!" case $1 in "one") printit hello-1 ;; "two") printit hell0-2 ;; "three") printit hello-3 ;; *) echo "Usage $0 {one|two|three}" ;; esac
输出:
asn@asn-vm:~/Desktop/scripts$ sh shell-fun.sh one This program will print your selection! Your choice is hello-1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/+pXV5xK8TEbHpxGrjIYsKdmEEeyaSjrPPNYSKmbysxNTnWxu4pCxkW+bPMl7S0aPQcx/sQkonQ0HohIVCUG0SUXdV2JsOWmYQT29/Pi97V+rKmtFAl8O+JC7T2NBbB5I4PvDMDjMGt9bN6Jt7HcYN1IH2qXBwyi1l+5iy4bY8H1kslWbNcXalooSSYEgO1YbPilvJuoAG5WSB27v77GwEsWs4uNeSjmbTf7qe5PfhH0+8YtJd6gNMYWzeWJLWRdYfUP4YwhIT5Um2cyqhggtbWX9aghRXGd8wJnfudCGRz765Itob24+L8t190T3yAgjFUnoAm2Vd5kgHMUq5HYd asn@asn-vm