简单的shell script范例

1、read键入名称,echo输出至屏幕中

read:键入;-p提示语。echo -e:自动转换转义字符
#!/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}" # 結果由螢幕輸出

2、利用date动态命名,touch创建文件

filename=${fileuser:-"filename"}   # 若fileuser值为空,则赋值为filename,否则赋原值。

+%Y%m%d必须是连续的,+与之前的字元必须有间隔
#!/bin/bash
# Program:
#	Program creates three files, which named by user's input and date command.
# History:
# 2015/07/16	VBird	First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 1. 讓使用者輸入檔案名稱,並取得 fileuser 這個變數;
echo -e "I will use 'touch' command to create 3 files." # 純粹顯示資訊
read -p "Please input your filename: " fileuser         # 提示使用者輸入

# 2. 為了避免使用者隨意按 Enter ,利用變數功能分析檔名是否有設定?
filename=${fileuser:-"filename"}           # 開始判斷有否設定檔名

# 3. 開始利用 date 指令來取得所需要的檔名了;
date1=$(date --date='2 days ago' +%Y%m%d)  # 前兩天的日期
date2=$(date --date='1 days ago' +%Y%m%d)  # 前一天的日期
date3=$(date +%Y%m%d)                      # 今天的日期
file1=${filename}${date1}                  # 底下三行在設定檔名
file2=${filename}${date2}
file3=${filename}${date3}

# 4. 將檔名建立吧!
touch "${file1}"                           # 底下三行在建立檔案
touch "${file2}"                           # " "可以省略
touch "${file3}"

3、简单的加减乘除

默认的计算方式或者declare -i 声明的方式均只能计算整形,float或者double会报错。
#!/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}"

5、if then判断语句

[ "${choice_yn}" == "Y" -o "${choice_yn}" == "y" ]等价于[ "${choice_yn}" == "Y" ] || [ "${choice_yn}" == "y" ] 
只不过一个是在同一个[ ]中判断,一个是在多个[ ]中判断。(-o:或;-a 和)
#!/bin/bash
# Program
#       this program shows the user's choice.
# History
# 2023/05/13 Tao First Release
# 

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 提示语句
read -p "please input your choice (Y/N): " choice_yn
if [ "${choice_yn}" == "Y" ] || [ "${choice_yn}" == "y" ]; then
        echo "OK, continue";   # 可以没有;
        exit 0;
elif [ "${choice_yn}" == "N" ] || [ "${choice_yn}" == "n" ]; then
        echo "Oh, exit";
        exit 0;
else
        echo "please input it again!" && exit 0;
# fi结尾
fi
~   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值