haHAL 发表于 2005-8-15 03:55:39

在redhat9里遇到一则bash脚本运行问题

mytar.sh -- a sample script:
#!/bin/bash

if [ "${1##*.}" = "tar" ]
then
      echo This appears to be a tarball.
else
      echo At first glance, this does not appear to be a tarball.
fi

然后给mytar.sh加上可执行属性:
$chmod 755 mytar.sh

运行该脚本:
$ ./mytar.sh thisfile.tar
提示出错:
./mytar.sh: line 3 : if [ tar = tar ]: command not found
./mytar.sh: line 4 : syntax error near unexpeced token `then'
./mytar.sh: line 4 : `then'

这个例子出自Daniel Robbins 的
《Bash by example, Part 1 Fundamental programming in the Bourne again shell (bash)》
http://www-106.ibm.com/developerworks/library/l-bash1.html

demonlj 发表于 2005-8-15 09:53:38

在我这里no problem!
[root@cold ~]# cat tmp
#!/bin/bash

if [ "${1##*.}" = "tar" ]
then
echo This appears to be a tarball.
else
echo At first glance, this does not appear to be a tarball.
fi
[root@cold ~]# ./tmp thisfile.tar
This appears to be a tarball.

demonlj 发表于 2005-8-15 09:55:42

你可以试试在then前加个";"
页: [1]
查看完整版本: 在redhat9里遇到一则bash脚本运行问题