1.获取本机IP #!/bin/bash a=`ifconfig|grep "Bcast"| awk {"print $2"}|cut -c 6-` echo "your PC"s IP is "$a 2.yes/no返回不同结构(case的应用 ) #!/bin/bash clear echo -n "enter [y/n]:" read a case $a in y|Y|Yes|YES|yes) echo "you enter $a";; n|N|No|no|No) echo "you enter $a";; *)echo "error";; esac 3.读取某一文件的指定行 #!/bin/bash file="/etc/passwd" for NUM in 2 4 6 19 13 ;do echo `sed -n"$NUM"p$file` done
4.给函数传递参数 #!/bin/bash #定义函数 p_num() { num=$1 echo $num } #使用for循环 for n in $* do #把循环到的参数传递到函数 p_num $n done ~
5.创建文件夹 #!/bin/bash while : do echo -n "please input file"s name:" read a if test -e /home/chensiyao/$a then echo "the file is existing.Please input new file name:" else mkdir /home/chensiyao/$a echo "you are sussesfull!" break fi done
6.二进制转十进制 #!/bin/sh # vim: set sw=4 ts=4 et: help() { cat <<HELP b2h -- convert binary to decimal USAGE: b2h [-h] binarynum OPTIONS: -h help text EXAMPLE: b2h 111010 will return 58 HELP exit 0 } error() { # print an error and exit echo "$1" exit 1 }
lastchar() { # is the last num if [ -z "$1" ];then rval="" return fi #取当前参数个数,忽略空格 numofchar=`echo -n "$1"|wc -c |sed "s/ //g"` #取参数最后一个字节 rval=`echo -n "$1"|cut -b $numofchar` #判断该值是否为1或0 if [ "$rval" != "1" -a "$rval" != "0" ];then echo "The number $1 is not a binnumorig!please check!!!" exit 1 else return fi }