我正在尝试在bash shell脚本中将grep用作变量和函数。当grep用作变量时,我得到所需的输出。将grep用作函数时,我没有得到想要的输出。
将grep用作变量的脚本如下:
#! /bin/bash
grep=$(grep -ico "mmshutdown: Finished" mmshutdown-output.txt)
#grep()
#{
# grep -ico "mmshutdown: Finished" mmshutdown-output.txt
#}
status()
{
echo $?
}
if [[ "grep" -gt "0" ]];
then
echo
echo "exit code of search mmshutdown: Finished is $(status)"
echo
echo "FILE SYSTEM UnmOUNTED SUCESSFULLY"
sleep 3
else
echo "exit code of search mmshutdown: Finished is $(status)"
echo "check output log file mmshutdown.txt"
sleep 3
fi
运行脚本时的输出是
[root@ad ~]# ./grep-variable.sh
exit code of search mmshutdown: Finished is 0
FILE SYSTEM UnmOUNTED SUCESSFULLY
函数中用于grep的脚本如下:
#! /bin/bash
#grep=$(grep -ico "mmshutdown: Finished" mmshutdown-output.txt)
grep()
{
grep -ico "mmshutdown: Finished" mmshutdown-output.txt
}
status()
{
echo $?
}
if [[ "grep" -gt "0" ]];
then
echo
echo "exit code of search mmshutdown: Finished is $(status)"
echo
echo "FILE SYSTEM UnmOUNTED SUCESSFULLY"
sleep 3
else
echo "exit code of search mmshutdown: Finished is $(status)"
echo "check output log file mmshutdown.txt"
sleep 3
fi
运行脚本时的输出是
[root@ad ~]# ./grep-function.sh
exit code of search mmshutdown: Finished is 1
check output log file mmshutdown.txt
有人可以指出这里出了什么问题吗?为什么将grep用作函数不能提供所需的输出?