作者/来源:yixinu.com
栏目:运维/编程
日期:2012-04-05 02:50:35
彩色shell输出:
example:防止密码被偷看
[root@fedora14 ~]# cat 6.sh #!/bin/bash echo -e "Please input password...�33[31;41m" read PASSWD echo -e "�33[0m" echo $PASSWD | openssl md5 [root@fedora14 ~]# ./6.sh Please input password... fsdfsafsfaf (stdin)= b8d97575383bdc8a3afe5e30f624461e [root@fedora14 ~]#
shell中的数学计算:
[root@fedora14 ~]# expr 6 + 3 9 [root@fedora14 ~]# echo | expr 6 + 3 9 [root@fedora14 ~]# echo | expr 6 / 3 2 [root@fedora14 ~]# echo | expr 6 * 3 expr: syntax error [root@fedora14 ~]# echo | expr 6 * 3 18 [root@fedora14 ~]# echo | expr 6 % 3 0 [root@fedora14 ~]#
判断之前执行的shell或命令是否执行成功
example1:
[root@fedora14 ~]# find /etc -name "*.conf" | wc -l 337 [root@fedora14 ~]# echo $? 0 [root@fedora14 ~]# cat /etc/httpd.conf2 cat: /etc/httpd.conf2: No such file or directory [root@fedora14 ~]# echo $? 1 [root@fedora14 ~]# ls /etc/httpd.conff ls: cannot access /etc/httpd.conff: No such file or directory [root@fedora14 ~]# echo $? 2 [root@fedora14 ~]#
获取从键盘输入的数据read
example1:
[root@fedora14 ~]# echo "Your name" Your name [root@fedora14 ~]# read NAME xianglou [root@fedora14 ~]# echo "Hello $NAME," Hello xianglou, [root@fedora14 ~]# echo "Hello $NAME," Hello xianglou, [root@fedora14 ~]# read NAME1 NAME2 aaaaaaaaaaaaaaaaaa [root@fedora14 ~]# echo $NAME1 aaaaaaaaaaaaaaaaaa [root@fedora14 ~]# echo $NAME2 [root@fedora14 ~]# read NAME1 NAME2 AAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBB [root@fedora14 ~]# echo $NAME1 AAAAAAAAAAAAAAAA [root@fedora14 ~]# echo $NAME2 BBBBBBBBBBBBBBBBBBB [root@fedora14 ~]#
通配符的使用
一行执行多个命令
example1:
[root@fedora14 ~]# date ;echo " "; cat names Thu Apr 5 11:15:20 PDT 2012 china beijing 123 America NewYork 456 England London 789 England London [root@fedora14 ~]#
命令行的参数
example1:
[root@fedora14 ~]# cat 7.sh #!/bin/bash echo "$0:$0" echo "$1:$1" echo "$2:$2" echo "Total:$#" echo "$@:$@" [root@fedora14 ~]# ./7.sh root fedora $0:./7.sh $1:root $2:fedora Total:2 $@:root fedora [root@fedora14 ~]#
shell脚本中的条件判断
shell的返回结果 0为true,可以理解为没有错误,1为false
example1:
[root@fedora14 ~]# vim 7.sh #!/bin/bash if [ 0 ];then echo "This is true" else echo "This is false" fi
[root@fedora14 ~]# ./7.sh This is true
测试条件,if/else,&&、||
example1: 用 ping 测试某台主机是否在线
[root@fedora14 ~]# vim 7.sh #!/bin/bash HOST=$2 if [ $# -ne 0 -a "$1" == "ok" ];then if ping -c2 $HOST&>/dev/null;then echo "$HOST is online" else echo "$HOST is down" fi else echo "Please input ....." fi [root@fedora14 ~]# ./7.sh ok 192.168.73.50 192.168.73.50 is online [root@fedora14 ~]#
example2: 用ping循环测试多台主机是否在线
[root@fedora14 ~]# vim 8.sh #!/bin/bash for TEMP in $@ do ping -c2 $TEMP &>/dev/null && echo "$TEMP is online" || echo "$TEMP is down" done
[fedora@fedora14 ~]$ [ -w ./test.file ] && echo "Files can reading" || echo "Files cannot writing"
case、函数
example1: 显示菜单
#!/bin/bash showmenu() { echo -e "1.linuxn2.windowsn3.unixn4.quit" read IN } testchoose() { if [ $IN == "" ];then echo "Please input 123" exit -1 else case $IN in 1) echo "welcome to linux";; #exit 1;; 2) echo "welcome to windows";; #exit 2;; 3) echo "welcome to unix";; #exit 3;; 4) exit 4;; esac fi } main() { IN=0 while [ "$IN" != "4" ] do showmenu testchoose done } main
example2: 过虑帐户
#!/bin/bash FILE="/etc/passwd" test() { if (($1<=0)); then echo "Please input...." else cat $FILE | grep $2 fi } test $# $1
文件测试操作
example1:测试文件是否存在
[root@bogon ~]# [ -e /etc/grub.conf ] && echo "Files can be reading" || echo "Files cannot reading" Files can be reading [root@bogon ~]# [ -e /etc/grub.confa ] && echo "Files can be reading" || echo "Files cannot reading" Files cannot reading [root@bogon ~]#
example2: