第十二章、学习 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}" # 结果由屏幕输出

在这里插入图片描述

12.3 善用判断式

file_perm.sh v1

#!/bin/bash

# Program:
#       User inputs his first name and last name.  Program shows his full name.

#!/bin/bash

read -p "输入一个文件名: " filename	# 提示使用者输入
if [ -e ${filename} ]
then
	if [ -f ${filename} ]
	then
		echo "${filename} is regular file" # 文件
	elif [ -d ${filename} ]
	then 
		echo "${filename} is directory" # 目录
	fi
	
	if [ -r ${filename} ]
	then
		echo "${filename} can be read" # 可读
	else
		echo "${filename} cannot be read"
	fi
else
	echo "${filename} does not exist"
fi

在这里插入图片描述

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} ]
then
	if [ -f ${filename} ]
	then
		filetype="regulare file" # 文件
	elif [ -d ${filename} ]
	then 
		filetype="directory" # 目录
	fi
	
	echo "${filename} is ${filetype}" # 文件类型
	
	if [ -r ${filename} ]
	then
		perm="readable"	# 可读
	elif [ -w ${filename} ]
	then
		perm="${perm} writable" # 可写
	elif [ -x ${filename} ]
        then
		perm="${perm} executable" # 可执行
	fi
	echo "The permissions of ${filename} are: ${perm}" # 文件所拥有的权限
else
	echo "${filename} does not exist"
	exit 1
fi

在这里插入图片描述
在这里插入图片描述
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 ]
then
        echo "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   ==> '$@'"

在这里插入图片描述
hello-2.sh

#!/bin/bash
# Program:
#	Check $1 is equal to "hello"

if [ "${1}" == "hello" ]; then
	echo "Hello, how are you?"
elif [ "${1}" == "" ]; then
	echo "You MUST input parameters, ex> {${0} someword}"
else 
	echo "The only parameter is 'hello', ex> {${0} hello}"
fi

在这里插入图片描述
hello-3.sh

#!/bin/bash
# Program:
# 	Show "Hello" from $1.... by using case .... esac

case ${1} in
	"hello")
		echo "Hello, how are you ?" ;;
	"")
		echo "You MUST input parameters, ex> {${0} someword}" ;;
	*)
		echo "Usage ${0} {hello}" ;;
esac

在这里插入图片描述

12.8 本章习题

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

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

script1.sh

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

在这里插入图片描述

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

在这里插入图片描述
在这里插入图片描述

  • 让用户输入一个数字,程序可以由 1+2+3… 一直累加到用户输入的数字为止。
    script3.sh
    while
#!/bin/bash

read -p "输入一个数字: " n
sum=0
i=0
while [ "${i}" != "${n}" ]
do
        i=$(($i+1))
        sum=$(($sum+$i))
done
echo "The result of 1+2+3+...+$n is ==> $sum"

在这里插入图片描述
for

#!/bin/bash

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

在这里插入图片描述

  • 撰写一支程序,他的作用是: 1.) 先查看一下 /root/test/logical 这个名称是否存在; 2.) 若不存在,则建立一个档案,使用 touch 来建立,建立完成后离开; 3.) 如果存在的话,判断该名称是否为档案,若为档案则将之删除后建立一个目录,文件名为 logical ,之后离开; 4.) 如果存在的话,而且该名称为目录,则移除此目录!
    script4.sh
#!/bin/bash

filename='/root/test/logical'
if [ -e $filename ]; then
        # 名称存在
        if [ -f $filename ]; then
                # 判断该名称是否为文件
                rm -f $filename
                mkdir $filename
        elif [ -d $filename ]; then
                # 名称为目录
                rm -rf $filename
        fi      
else
        touch $filename
        exit 1
fi

在这里插入图片描述

  • 我们知道 /etc/passwd 里面以 : 来分隔,第一栏为账号名称。 请写一只程序,可以将 /etc/passwd 的第一栏取出,而且每一栏都以一行字串『The 1 account is “root” 』来显示,那个 1 表示行数。
    script5.sh
#!/bin/bash

accounts=$(cat /etc/passwd | cut -d ":" -f1)

for account in $accounts
do      
        declare -i i=$i+1
        echo "The $i account is \"$account\""
done