#!/bin/bash
# Program:
# User inputs his first name and last name. Program shows his full name.
# History:
# 2015/07/16 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input your first name: " firstname # 提示使用者輸入
read -p "Please input your last name: " lastname # 提示使用者輸入
echo -e "\nYour full name is: ${firstname} ${lastname}" # 結果由螢幕輸出
#!/bin/bash
# Program
# this program is used cal multiplying.
# History
# 2023/5/12 Tao First release
#
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 提示输入乘数和被乘数
echo -e "PLease input 2 numbers,we will multiplying them! \n"
read -p "Please input first number: " firstnum
read -p "Please input second number: " secondnum
# 对输入的两个数值进行计算 declare -r var可指定变量为int整形
# 即使未使用declare声明,默认仍为整形计算.小数计算需使用|bc.
declare i total=$((${firstnum}*${secondnum}))
echo -e "\nThe result of ${firstnum} and ${secondnum} is ${total}"
4、利用test指令的测试功能
test -z:判断字符串是否为空。!代表相反指令,即若文件不存在则返回true。
A || B && C,相当于(A || B) && C,如果A成立则B不会执行,那么&&左侧相当于true,C会被执行。
#!/bin/bash
# Program
# when user input filename,this program will check the following:
# 1>exist? 2>file/directory? 3>file permissions
# History
# 2023/05/12 Tao First release
#
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1.请用户输入需要查询的文件名,并首先判断是否输入成功
echo -e "Please input filename,i will check the filename's type and permission.\n"
read -p "Please input the filename: " filename
# test -z 判断字符串是否为空 exit 0:如果为空,则退出后续的执行
test -z ${filename} && echo -e "you must input filename" && exit 0
# 对输入的filename进行判断是否存在,若不存在直接推出
test ! -e ${filename} && echo "the filename ${filename} do not exist" && exit 0
# 判断文件的文档类型和权限属性
test -f ${filename} && filetype="regulare file"
test -d ${filename} && filetype="directory"
test -r ${filename} && perm="readable"
test -w ${filename} && perm="${perm} writeable"
test -x ${filename} && perm="${perm} executable"
# 将获得的信息输出
echo "the filename: ${filename} is a ${filetype}"
echo "and the permission for you are: ${perm}"