学习笔记备忘
- case的语法
PATH=${PATH}:~/bin
export PATH
read -p "please input:" word
case $word in
"hello")
echo "hi"
;;
"") #没有输入
echo "need input"
;;
*) #代表任意符号的意思
echo "input error"
;;
esac
- date的格式化
#!/bin/bash
#This is bash for test make file
#Author:CHJ
PATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/bin
export PATH
echo '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) #tomorrow
file1=$FILENAME$date1
file2=$FILENAME$date2
file3=$FILENAME$date3
touch "$file1"
touch "$file2"
touch "$file3"
exit 0
- if条件判断 do循环 for循环应用
#!bin/bash
#just test for check file
#Author:CHJ
PATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/bin
export PATH
read -p "please input a dir:" dir
#检查dir是否存在
#if [ $dir == "" -o ! -d "$dir" ];then #等价下面一句
if [ $dir == "" ] || [ ! -d "$dir" ] ;then
echo "not exist"
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 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))
do
s=$(($s+$i))
done
echo "result of sum is $s"
- 函数的使用
#!/bin/bash
PATH=${PATH}:~/bin
export PATH
echo "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" == "" ];then
echo "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 ];then
echo "already home"
else
echo "still $date_d days to home"
fi
- for循环
#!bin/bash
#just test for loop for
#Author:CHJ
PATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/bin
export PATH
for animal in dog cat elephant
do
echo "There are ${animal}s ```"
done
users=$(cut -d ':' -f1 /etc/passwd) #以:作分隔符,取第一个field(字段),即用户名
for username in $users
do
id $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 ];then
echo "Server ${network}.${net} is OK."
else
echo "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:CHJ
PATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/bin
export PATH
s=0 #sum
i=0 #每次累加的内容
#while [ $i != 100 ]#与下面等价
until [ $i == 100 ]
do
i=$(($i+1))
s=$(($s+$i)) #s代表变量 $s代表变量的值
done
echo "The result is $s"
- 脚本的语法检查
使用sh -x 查看运行过程
使用sh -n 查看是否有语法错误
PS:完整代码文件之后上传