鸟哥的私房菜Linux 学习笔记之 Bash语法

来源:互联网 发布:开淘宝网店的流程2016 编辑:程序博客网 时间:2024/05/29 17:45

学习笔记备忘

  • case的语法
PATH=${PATH}:~/binexport PATHread -p "please input:" wordcase $word in"hello")echo "hi";;"") #没有输入echo "need input";;*)  #代表任意符号的意思echo "input error";;esac
  • date的格式化
 #!/bin/bash#This is bash for test make file#Author:CHJPATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/binexport PATHecho 'will new 3 files'read -p 'please input your file name: ' fileuserFILENAME=${fileuser:-"Default"} #如果fileuser未定义,FILENAME默认值为Default#使用date命名文件date1=$(date --date='-1 days' +%Y-%m-%d-%H%M%S) #1 day agodate2=$(date +%Y-%m-%d-%H%M%S)                  #todaydate3=$(date --date='+1 days' +%Y-%m-%d-%H%M%S) #tomorrowfile1=$FILENAME$date1file2=$FILENAME$date2file3=$FILENAME$date3touch "$file1"touch "$file2"touch "$file3"exit 0
  • if条件判断 do循环 for循环应用
#!bin/bash#just test for check file#Author:CHJPATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/binexport PATHread -p "please input a dir:" dir#检查dir是否存在#if [ $dir == "" -o ! -d "$dir" ];then #等价下面一句if [ $dir == "" ] || [ ! -d "$dir" ] ;then    echo "not exist"    exit 1fi#检查文件权限filelist=$(ls $dir)for filename in $filelistdo    perm=""    test -r "$dir/$filename" && perm="$perm readable"    test -w "$dir/$filename" && perm="$perm writabe"    test -x "$dir/$filename" && perm="$perm executable"    echo "the file $dir/$filename's permission is $perm"done#计算100以内的奇数和s=0for((i=1;i<100;i=i+2))do    s=$(($s+$i))doneecho "result of sum is $s"
  • 函数的使用
#!/bin/bashPATH=${PATH}:~/binexport PATHecho "input is $1"function printit(){    echo "your input is $1"}case $1 in"one")printit 1 ;;"two") printit 2;;"three") printit; echo $1 | tr 'a-z' 'A-Z' #替换小写为大写 ;;*)  #代表任意符号的意思printit "wrong format";;esac#函数的 $1与Shell的$1是完全不同的 函数的$1 $2是跟在函数调用者时后的参数 比如printit "wrong format" "haha"#"wrong format"代表$1 "haha"代表$2
  • 计算日期的差值
 #!/bin/bash#bash to calculate how many days to back home# tell user to input the day they want to go homeecho "please input the day they want to go home"read -p "please input date (YYYYMMDD ex:20180808):" date2#judge wether input rightdate_d=$(echo $date2 | grep  '[0-9]\{8\}')if [ "$date_d" == "" ];then    echo "wrong format"    exit 1fi# start calculatedeclare -i date_dem=$(date --date="$date2" +%s)#declare -i date_now='date +%s' #会莫名其妙报错declare -i date_now=$(date +%s)declare -i date_total_s=$(($date_dem - $date_now))declare -i date_d=$(($date_total_s/60/60/24))if [ $date_total_s -lt 0 ];then    echo "already home"else    echo "still $date_d days to home"fi
  • for循环
 #!bin/bash#just test for loop for#Author:CHJPATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/binexport PATHfor animal in dog cat elephantdo    echo "There are ${animal}s ```"doneusers=$(cut -d ':' -f1 /etc/passwd) #以:作分隔符,取第一个field(字段),即用户名for username in $usersdo    id $username #使用用户名获取一些用户信息    finger $username #使用用户名获取一些用户信息done#ping netstate from 172.19.7.1 to 172.19.7.100network="172.19.7"for net in $(seq 1 100) #1~100do     ping -c 1 -w 1 ${network}.${net} &> /dev/null && result=0 || result=1 #-c指ping的次数 -w 指等待时常    if [ $result == 0 ];then        echo "Server ${network}.${net} is OK."    else        echo "Server ${network}.${net} is NG."    fidone#ping -c 1 -w 1 ${network}.${net} &> /dev/null#相当于#ping -c 1 -w 1 ${network}.${net} > /dev/null 2>&1#2是标准错误(就是错误信息)#1是正常输出(正常结果)#意思就是把ping -c 1 -w 1 ${network}.${net} 执行完输出的东西都放入/dev/null里#不管出错了,还是正常ping通了,只要是输出到屏幕的信息都放入/dev/null里#/dev/null是个垃圾站
  • until循环
 #!bin/bash#just test for loop until#Author:CHJPATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/binexport PATHs=0 #sumi=0 #每次累加的内容#while [ $i != 100 ]#与下面等价until [ $i == 100 ]do  i=$(($i+1))  s=$(($s+$i)) #s代表变量 $s代表变量的值doneecho "The result is $s"
  • 脚本的语法检查
    使用sh -x 查看运行过程
    使用sh -n 查看是否有语法错误

PS:完整代码文件之后上传

阅读全文
0 0