当前位置: 技术问答>linux和unix
今天运行鸟叔书上shell script代码时遇的问题
来源: 互联网 发布时间:2017-05-15
本文导语: 我把代码完整放上,主要是第20行和22行,我想问下:$只后,为什么有时候加两对括号有时候只加一对? #!/bin/bash # Program: # You input your demobilization date, I calculate how many days # before you demobilize. # Histo...
我把代码完整放上,主要是第20行和22行,我想问下:$只后,为什么有时候加两对括号有时候只加一对?
#!/bin/bash
# Program:
# You input your demobilization date, I calculate how many days
# before you demobilize.
# History:
# 2014/04/13 vampire First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "This program will try to calculate :"
echo "How many days before your demobilization date..."
read -p "Please input your demobilization date (YYYYMMDD ex>20140413) :" date2
date_d=$(echo $date2 | grep '[0-9]{8}')
if [ "$date_d" == "" ]; then
echo "You input the wrong date format...."
exit 1
fi
declare -i date_dem=$(date --date="$date2" +%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 demobilization before: "$((-1*$date_d)) "ago"
else
declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60))
echo "You will demobilize after $date_d days and $date_h hours."
fi
|
多熟悉语法就好了
declare -i date_dem=$(date --date="$date2" +%s)
这个是单纯的命令替换,是把date命令执行的结果赋值给变量date_dem
declare -i date_total_s=$(($date_dem-$date_now))
这个是要进行算术运算,把变量date_dem和date_now相减的结果赋值给变量date_total_s
declare -i date_dem=$(date --date="$date2" +%s)
这个是单纯的命令替换,是把date命令执行的结果赋值给变量date_dem
declare -i date_total_s=$(($date_dem-$date_now))
这个是要进行算术运算,把变量date_dem和date_now相减的结果赋值给变量date_total_s
|
A=($date_dem-$date_now)
这是变量A的定义
$A这是取值
$($date_dem-$date_now) ——这个就不对了
这是变量A的定义
$A这是取值
$($date_dem-$date_now) ——这个就不对了
|
declare -i date_total_s=$(($date_dem-$date_now))
A=($date_dem-$date_now)
$(A)//书写规范吧