本文主要是介绍鸟哥的私房菜Linux 学习笔记之 Bash语法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
学习笔记备忘
- 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:~/bin
export PATHecho 'will new 3 files'read -p 'please input your file name: ' fileuser
FILENAME=${fileuser:-"Default"} #如果fileuser未定义,FILENAME默认值为Default#使用date命名文件
date1=$(date --date='-1 days' +%Y-%m-%d-%H%M%S) #1 day ago
date2=$(date +%Y-%m-%d-%H%M%S) #today
date3=$(date --date='+1 days' +%Y-%m-%d-%H%M%S) #tomorrowfile1=$FILENAME$date1
file2=$FILENAME$date2
file3=$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:~/bin
export PATHread -p "please input a dir:" dir
#检查dir是否存在
#if [ $dir == "" -o ! -d "$dir" ];then #等价下面一句
if [ $dir == "" ] || [ ! -d "$dir" ] ;thenecho "not exist"exit 1
fi#检查文件权限
filelist=$(ls $dir)
for filename in $filelist
doperm=""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=0
for((i=1;i<100;i=i+2))
dos=$(($s+$i))
done
echo "result of sum is $s"
- 函数的使用
#!/bin/bash
PATH=${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 home
echo "please input the day they want to go home"
read -p "please input date (YYYYMMDD ex:20180808):" date2#judge wether input right
date_d=$(echo $date2 | grep '[0-9]\{8\}')
if [ "$date_d" == "" ];thenecho "wrong format"exit 1
fi# start calculate
declare -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 ];thenecho "already home"
elseecho "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:~/bin
export PATHfor animal in dog cat elephant
doecho "There are ${animal}s ```"
doneusers=$(cut -d ':' -f1 /etc/passwd) #以:作分隔符,取第一个field(字段),即用户名
for username in $users
doid $username #使用用户名获取一些用户信息finger $username #使用用户名获取一些用户信息
done#ping netstate from 172.19.7.1 to 172.19.7.100
network="172.19.7"
for net in $(seq 1 100) #1~100
do ping -c 1 -w 1 ${network}.${net} &> /dev/null && result=0 || result=1 #-c指ping的次数 -w 指等待时常if [ $result == 0 ];thenecho "Server ${network}.${net} is OK."elseecho "Server ${network}.${net} is NG."fi
done#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:~/bin
export PATHs=0 #sum
i=0 #每次累加的内容
#while [ $i != 100 ]#与下面等价
until [ $i == 100 ]
doi=$(($i+1))s=$(($s+$i)) #s代表变量 $s代表变量的值
done
echo "The result is $s"
- 脚本的语法检查
使用sh -x 查看运行过程
使用sh -n 查看是否有语法错误
PS:完整代码文件之后上传
这篇关于鸟哥的私房菜Linux 学习笔记之 Bash语法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!