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/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
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
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 ==> '$@'"
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
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
(要看答案请将鼠标移动到答:'底下的空白处,按下左键圈选空处即可察看) 底下皆为实作题,请自行撰写出程序喔!
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" ]; 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
#!/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"
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"
#!/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
#!/bin/bashaccounts=$(cat /etc/passwd | cut -d ":" -f1)for account in $accounts
do declare -i i=$i+1echo "The $i account is \"$account\""
done