shell 练习

1.用户输入日期
2.当前日期对比输入的日期
3.计算输入日期减去当前日期的时间

#!/bin/bash
read -p "please input the date you demobilizete (YYYYMMDD ex=> 20090422): " date1
date_d=$(echo $date1 | grep '[0-9]\{8\}') #正则表达式
if [ "$date_d" == "" ]; then
echo "you input the wrong date format......."
exit 1
fi
declare -i date_dem='date --date="$date1" +%s'  
declare -i date_now='date +%s'
declare -i date_total_s=$(($date_dem-$date_now))
declare -i date_d=$(($date_total_s/60/60/24))
if [ "$date_total_s" -lt "0" ]; then
echo "you had been demobiliation before : " $((-l*$date_d)) "ago"
else
declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60))
echo "you will demoblize after $date_d days and $date_h hours."
fi


#报错
#please input the date you demobilizete (YYYYMMDD ex=> 20090422): 20120514
#./sh09.sh: line 8: declare: date --date="$date1" +%s: syntax error in expression (error token is "date="$date1" +%s")
#./sh09.sh: line 9: declare: date +%s: syntax error: operand expected (error token is "%s")
#./sh09.sh: line 10: -: syntax error: operand expected (error token is "-")
#./sh09.sh: line 11: /60/60/24: syntax error: operand expected (error token is "/60/60/24")
#./sh09.sh: line 12: [: : integer expression expected
#you will demoblize after 20120514 days and -482892336 hours.

Other:
#!/bin/bash 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH  
echo -e "you should input 2 numbers, i will cross then! \n" 
read -p "first number:" firstnumber
read -p "second number:" secondnumber 
total=$(($firstnumber*$secondnumber)) 
echo -e "\n result of $firstnumber * $secondnumber is ==> $total"


这个有点不懂的地方
#!/bin/bash
read -p "input your filename:" fileuser
filename=${fileuser:-"filename"}
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}
touch "$file1"
touch "$file2"
touch "$file3"


#!/bin/bash
echo -e "please input a filename,i will check the filename's type and permission. \n\n"
read -p "input a filename:" filename
test -z $filename && echo "you must input a filename " && exit 0
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 writable"
test -x $filename && perm="$perm executable"
echo "the filename: $filename is a $filetype"
echo "and the permission are : $perm"


#!/bin/bash
#read -p "please input y or n:" sign
#[ "$sign" == "Y" -o "$sign" == "y" ] && echo "OK,continue " && exit 0
#[ "$sign" == "N" -o "$sign" == "n" ] && echo "OH,interrupt" && exit 0
#echo "fuck you" && exit 0

read -p "please rape me :" rape
if [ "$rape" == "Y" ] || [ "$rape" == "y" ]; then
echo "ok , so fuck "
exit 0
fi
if [ "$rape" == "N" ] || [ "$rape" == "n" ]; then
echo "no , interrupt "
exit 0
fi
echo " i don't know whar your choice is " && exit 0 



#!/bin/bash
echo "the script name is  : $0 "
echo "total parameter number is : $# "
[ "$#" -lt 2 ] && echo "the number of parameter is less than 2. stop here." && exit 0
echo "all parameters is : '$@'"
echo "the 1st parameter is : $1 "
echo "the 2nd parameter is : $2 "


#!/bin/bash
echo "total parameter number is ==> $# "
echo "your whole parameter is ==> '$@'"
shift 
echo " total parameter number is ++> $# "
echo " your whole parameter is ++> '$@' "
shift 3
echo " total parameter number is ++> $# "
echo


### 关于 Shell 脚本练习题及学习资源 以下是关于 Shell 脚本的一些经典练习题目以及推荐的学习资源: #### 经典 Shell 脚本练习题 1. **持续运行脚本直到出错** 实现一个能够不断运行指定脚本,直到该脚本返回错误状态为止的 Bash 脚本。此脚本应捕获标准输出和错误流,并将其记录到文件中。最终还需统计脚本失败前总共运行了多少次[^2]。 ```bash #!/bin/bash count=0 log_file="script_output.log" while true; do ./your_script.sh > >(tee -a "$log_file") 2> >(tee -a "$log_file" >&2) if [ $? -ne 0 ]; then echo "Script failed after $count runs." >> "$log_file" break fi ((count++)) done ``` 2. **字符串分割成数组** 使用 `awk` 和其他工具将特定格式的数据解析为数组形式。例如,从系统事实数据中提取 IP 地址并存储至数组中[^3]。 ```bash result=$(facter | awk '/ipaddress/ && !/ipaddress_lo/{print $1, $3}') array=($result) for ip in "${array[@]}"; do echo "IP Address: $ip" done ``` 3. **位置变量的应用** 创建一个接受多个参数的脚本,利用 `$1`, `$2`, 等位置变量处理传入的参数,并打印这些参数的内容[^4]。 ```bash #!/bin/bash echo "First argument is: $1" echo "Second argument is: $2" echo "All arguments are: $@" ``` 4. **MySQL 数据库操作** 编写一个用于创建数据库、表结构并插入数据的 Shell 脚本。通过管道输入 SQL 命令完成整个流程[^5]。 ```bash #!/bin/bash mysql -u root -p'password' <<EOF CREATE DATABASE IF NOT EXISTS testdb; USE testdb; CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY, name VARCHAR(50)); INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob'); SELECT * FROM users; EOF ``` #### 推荐 Shell 学习资源 - **书籍**:《Advanced Bash-Scripting Guide》是一份详尽的指南,涵盖了从基础语法到高级技巧的各种主题。 - **在线课程**: Coursera 或 Udemy 提供了许多针对初学者和中级用户的 Shell 编程课程。 - **官方文档**: GNU Bash 官方手册提供了权威性的参考资料,适合深入研究具体功能和技术细节。 - **社区论坛**: Stack Overflow 是解决实际编码问题的好地方;而 Reddit 的 r/bash 版块则经常分享实用的小贴士和经验交流。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值