https://linux.vbird.org/linux_basic/centos7/0340bashshell-scripts.php
#!/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}" # 结果由屏幕输出
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
#!/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 ==> '$@'"
#!/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
#!/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
(要看答案请将鼠标移动到答:'底下的空白处,按下左键圈选空处即可察看) 底下皆为实作题,请自行撰写出程序喔!
script1.sh
#!/bin/bash
echo -e "目前的身份: $(whoami)\n"
echo "当前所在目录: $(pwd)"
#!/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
#!/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"
#!/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"
#!/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
#!/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