> 文章列表 > 鸟哥的Linux私房菜 Shell脚本

鸟哥的Linux私房菜 Shell脚本

鸟哥的Linux私房菜 Shell脚本

第十二章、学习 Shell Scripts

https://linux.vbird.org/linux_basic/centos7/0340bashshell-scripts.php

12.2 简单的 shell script 练习

#!/bin/bash# Program:
#       User inputs his first name and last name.  Program shows his full name.read -p "Please input your first name: " firstname      # 提示使用者输入
read -p "Please input your last name:  " lastname       # 提示使用者输入echo -e "\\nYour full name is: ${firstname} ${lastname}" # 结果由屏幕输出

鸟哥的Linux私房菜 Shell脚本

12.3 善用判断式

file_perm.sh v1

#!/bin/bash# Program:
#       User inputs his first name and last name.  Program shows his full name.#!/bin/bashread -p "输入一个文件名: " filename	# 提示使用者输入
if [ -e ${filename} ]
thenif [ -f ${filename} ]thenecho "${filename} is regular file" # 文件elif [ -d ${filename} ]then echo "${filename} is directory" # 目录fiif [ -r ${filename} ]thenecho "${filename} can be read" # 可读elseecho "${filename} cannot be read"fi
elseecho "${filename} does not exist"
fi

鸟哥的Linux私房菜 Shell脚本

file_perm.sh v2

#!/bin/bash
# Program:
#	User input a filename, program will check the flowing:
#	1.) exist? 2.) file/directory? 3.) file permissions echo -e "Please input a filename, I will check the filename's type and permission. \\n\\n" # -e to \\n
read -p "Input a filename: " filename	# 提示使用者输入
if [ -e ${filename} ]
thenif [ -f ${filename} ]thenfiletype="regulare file" # 文件elif [ -d ${filename} ]then filetype="directory" # 目录fiecho "${filename} is ${filetype}" # 文件类型if [ -r ${filename} ]thenperm="readable"	# 可读elif [ -w ${filename} ]thenperm="${perm} writable" # 可写elif [ -x ${filename} ]thenperm="${perm} executable" # 可执行fiecho "The permissions of ${filename} are: ${perm}" # 文件所拥有的权限
elseecho "${filename} does not exist"exit 1
fi

鸟哥的Linux私房菜 Shell脚本
鸟哥的Linux私房菜 Shell脚本
shift_paras.sh

#!/bin/bash
# Program:
#       Program shows the script name, parameters...echo "The script name is        ==> ${0}"
echo "Total parameter number is ==> $#"
echo "Your whole parameter is   ==> '$@'"
shift # 第一次 偏移
if [ "$#" -lt 2 ]
thenecho "The number of parameter is less than 2.  Stop here." exit 0
fi
echo "Total parameter number is ==> $#"
echo "Your whole parameter is   ==> '$@'"
shift 3 # 第二次 偏移
echo "Total parameter number is ==> $#"
echo "Your whole parameter is   ==> '$@'"

鸟哥的Linux私房菜 Shell脚本
hello-2.sh

#!/bin/bash
# Program:
#	Check $1 is equal to "hello"if [ "${1}" == "hello" ]; thenecho "Hello, how are you?"
elif [ "${1}" == "" ]; thenecho "You MUST input parameters, ex> {${0} someword}"
else echo "The only parameter is 'hello', ex> {${0} hello}"
fi

鸟哥的Linux私房菜 Shell脚本
hello-3.sh

#!/bin/bash
# Program:
# 	Show "Hello" from $1.... by using case .... esaccase ${1} in"hello")echo "Hello, how are you ?" ;;"")echo "You MUST input parameters, ex> {${0} someword}" ;;*)echo "Usage ${0} {hello}" ;;
esac

鸟哥的Linux私房菜 Shell脚本

12.8 本章习题

(要看答案请将鼠标移动到答:'底下的空白处,按下左键圈选空处即可察看) 底下皆为实作题,请自行撰写出程序喔!

  • 请建立一支 script ,当你执行该 script 的时候,该 script 可以显示: 1. 你目前的身份 (用 whoami ) 2. 你当前所在目录 (用 pwd)

script1.sh

#!/bin/bash
echo -e "目前的身份: $(whoami)\\n"
echo "当前所在目录: $(pwd)"

鸟哥的Linux私房菜 Shell脚本

  • 请自行建立一支程序,该程序可以用来计算’你还有几天可以过生日’啊?
    script2.sh
#!/bin/bash
read -p "Pleas input your birthday (MMDD, ex> 0709): " bir
now=$(date +%m%d)
if [ "$bir" == "$now" ]; thenecho "Happy Birthday to you!!!"
elif [ "$bir" -gt "$now" ]; thenyear=$(date +%Y)total_d=$(($(($(date --date="$year$bir" +%s)-$(date +%s)))/60/60/24))echo "Your birthday will be $total_d later"
elseyear=$(($(date +%Y)+1))total_d=$(($(($(date --date="$year$bir" +%s)-$(date +%s)))/60/60/24))echo "Your birthday will be $total_d later"
fi

鸟哥的Linux私房菜 Shell脚本
鸟哥的Linux私房菜 Shell脚本

  • 让用户输入一个数字,程序可以由 1+2+3… 一直累加到用户输入的数字为止。
    script3.sh
    while
#!/bin/bashread -p "输入一个数字: " n
sum=0
i=0
while [ "${i}" != "${n}" ]
doi=$(($i+1))sum=$(($sum+$i))
done
echo "The result of 1+2+3+...+$n is ==> $sum"

鸟哥的Linux私房菜 Shell脚本
for

#!/bin/bashread -p "输入一个数字: " n
sum=0
i=0
for (( i=1; i<=$n; i++ ))
dosum=$(($sum+$i))
done
echo "The result of '1+2+3+...+${n}' is ==> $sum"

鸟哥的Linux私房菜 Shell脚本

  • 撰写一支程序,他的作用是: 1.) 先查看一下 /root/test/logical 这个名称是否存在; 2.) 若不存在,则建立一个档案,使用 touch 来建立,建立完成后离开; 3.) 如果存在的话,判断该名称是否为档案,若为档案则将之删除后建立一个目录,文件名为 logical ,之后离开; 4.) 如果存在的话,而且该名称为目录,则移除此目录!
    script4.sh
#!/bin/bashfilename='/root/test/logical'
if [ -e $filename ]; then# 名称存在if [ -f $filename ]; then# 判断该名称是否为文件rm -f $filenamemkdir $filenameelif [ -d $filename ]; then# 名称为目录rm -rf $filenamefi      
elsetouch $filenameexit 1
fi      

鸟哥的Linux私房菜 Shell脚本

  • 我们知道 /etc/passwd 里面以 : 来分隔,第一栏为账号名称。 请写一只程序,可以将 /etc/passwd 的第一栏取出,而且每一栏都以一行字串『The 1 account is “root” 』来显示,那个 1 表示行数。
    script5.sh
#!/bin/bashaccounts=$(cat /etc/passwd | cut -d ":" -f1)for account in $accounts
do      declare -i i=$i+1echo "The $i account is \\"$account\\""
done    

鸟哥的Linux私房菜 Shell脚本