下面记录linux里shell script学习过程中的一些笔记:
1:
根据输入名及时间信息,创建几个空文件
#!/bin/bash
#programe:
# This program creates three files, which named by user's input and date command
#History:
#2016/4/29 dairen First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#1. 让用户输入文件名
echo -e "I will use 'touch' command to creat 3 files."
read -p "filename:" fileuser
# 2. 为了避免使用者随意按 Enter,在这里检测fileuser是不是空或者未设置(bash变量设定的部分)
filename=${fileuser:-"filename"} #详见p316
# 3. 开始利用 date 命令来取得所需要的档名
date1=$(date --date='2 days ago' +%Y%m%d) # 前两天的日期
date2=$(date --date='1 days ago' +%Y%m%d) # 前一天的日期
date3=$(date +%Y%m%d%H%M%S) # 今天的日期hour minute second
file1=${filename}${date1} # 变量相加
file2=${filename}${date2}
file3=${filename}${date3}
# 4.
touch "$file2" # 创建空文件
touch "$file2"
touch "$file3"
#中文输入super+空格
man 命令是个好东西,要经常使用
2:
total=$(($firstnu*$secnu))
echo -e "\nThe result of $firstnu x $secnu is: $total"
其中对于数值运算可以:
declare -i total=$firstnu*$secnu
但是他们都不能进行小数运算,因为bash shell 里面默认支持到整数的数据
3:
test的使用
#!/bin/bash
# Program:
# User input a filename, program will check the flowing:
# 1.) exist? 2.) file/directory? 3.) file permissions
#history:
#2016/5/3 dairen First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1. 让使用者输入档名,并且判断使用者是否真的有输入字串?
echo -e "Please input a filename, I will check the filename's type and \
permission. \n\n"
read -p "Input a filename : " filename
test -z $filename && echo "You MUST input a filename." && exit 0
# 2. 判断文件是否存在?若不存在则显示信息并结束脚本
test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0
# 3. 开始判断文件类型与属性
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable" #变量相加
test -x $filename && perm="$perm executable"
# 4. 开始输出资讯!
echo "The filename: $filename is a $filetype"
echo "And the permissions are : $perm"
4:
【】
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:usr/local/sbin:~/bin
read -p "input y/n " yn
[ "$yn" == "y" -o "$yn" == "Y" ] && echo "is y" && exit 0
[ "$yn" == "n" -o "$yn" == "N" ] && echo "is n" && exit 0
echo "i don't know what your choice is"
5:
带参数的shell脚本
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "The script name is ==> $0"
echo "Total parameter number is ==> $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." \
&& exit 0
echo "Your whole parameter is ==> '$@'"
echo "The 1st parameter ==> $1"
echo "The 2nd parameter ==> $2"
6:
shift来使参数变量号码偏移
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift # 进行第一次『一个变量的 shift 』
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift 3 # 进行第二次『三个变量的 shift 』
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
7:
netstat 查看目前主机打开的网络接口有哪些
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
testing=$(netstat -tuln | grep ":80")
if [ "$testing" != "" ];then
echo "www is running in your system"
fi
testing=$(netstat -tuln | grep ":22")
if [ "$testing" != "" ];then
echo "ssh is running in your system"
fi
testing=$(netstat -tuln | grep ":21")
if [ "$testing" != "" ];then
echo "ftp is running in your system"
fi
testing=$(netstat -tuln | grep ":25")
if [ "$testing" != "" ];then
echo "mail is running in your system"
fi
testing=$(netstat -tuln | grep ":631")
if [ "$testing" != "" ];then
echo "cups is running in your system"
fi
exit 0
8:根据输入的date和现在的date,计算出天数
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "calculate days between two date"
read -p "inpute a date in the futere .ex>20170809" date2
stringtest=$(echo $date2 | grep '[0-9]\{8\}')
echo "$date2 $stringtest"
if [ "$stringtest" == "" ];then
echo "format wrong!!"
exit 1
fi
declare -i date_future=$(date --date="$date2" +%s)
declare -i date_now=`date +%s`
declare -i date_s=$(($date_future-$date_now))
if [ "$date_s" -lt "0" ];then
echo "your inpute date is not in the future"
else
declare -i date_days=$((date_s/60/60/24))
echo "days between now and your date is:"$date_days"."
fi
9:while do done、 until do done
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
while [ "$testing" == "" ]
do
read -p 'inpute a number please' num
testing=$(echo $num | grep '[0-9]')
done
s=0
i=0
while [ "$i" != "$num" ]
do
s=$(($s+$i))
i=$(($i+1))
done
echo "the sum from 0-num is: $s"
10:列出指定目录下所有文件的权限
#!/bin/bash
# Program:
# User input dir name, I find the permission of files.
# History:
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1. 先看看这个目录是否存
read -p "Please input a directory: " dir
if [ "$dir" == "" -o test ! -d "$dir" ]; then
echo "The $dir is NOT exist in your system."
exit 1
fi
# 2. 开始测试文件
filelist=$(ls $dir) # 列出所有在该目录下的文件名称
for filename in $filelist
do
perm=""
test -r "$dir/$filename" && perm="$perm readable"
test -w "$dir/$filename" && perm="$perm writable"
test -x "$dir/$filename" && perm="$perm executable"
echo "The file $dir/$filename's permission is $perm "
done
11:使用ping命令判断网络状态 seq 1 100 、 for do done、ping -c 1 -w 1 192.168.1.n
rogram
# Use ping command to check the network's PC state.
# History
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
network="192.168.1" # 先定义一个网域的前面部分!
for sitenu in $(seq 1 100) # seq 为 sequence(连续) 的缩写之意
do
# 底下的程序在取得 ping 的回传值是正确的还是失败的!
#发送一次包,间隔1s,返回结果重定向到无底洞
ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1
# 开始显示结果是正确的启动 (UP) 还是错误的没有连通 (DOWN)
if [ "$result" == 0 ]; then
echo "Server ${network}.${sitenu} is UP."
else
echo "Server ${network}.${sitenu} is DOWN."
fi
done
12:
13: