很不幸,在ubuntu12.04下不能执行以下shell代码,当使用:
sh test.sh
命令时,会得到一条语句:
test.sh: 3: test.sh: [[: not found
#!/bin/bash
LANG=C
if [[ -z "$1" ]]
then
if [[ -p /dev/stdin ]] # input from a pipe
then
read -r p
else
echo "No timestamp given." >&2
exit
fi
else
p=$1
fi
date -d @$p +%c
类似的其它错误还可能有:
test.sh: 7: test.sh: Syntax error: Bad for loop variable
方法一:
而在centos下则不会出现这个问题,原来系统默认的sh可能不是bash,尝试使用
bash test.sh
程序就正常运行了
可以做一个别名替换
alias sh="bash"
再执行
sh test.sh
程序仍然正常运行。
方法二:细心的话,看到第一行已经指定了程序使用哪个程序来执行shell脚本,大多数时候bash存储位置为/bin/bash。所以,只要执行以下命令即可:
./test.sh
如果文件没有执行权限时,使用:chmod 777 test.sh修改一下。