壳牌给我这个:proiect1.sh:3:[:非法号码:ilir1477

时间:2014-04-08 13:00:34

标签: bash shell

read -p "Dati user:(0 for over) " user

while [ $user -ne "0" ]
 do
   FullName=`grep $user /passwd | awk -F: '{print $5}'`
   NrProcese=`ps | grep -c $user`

   echo -e "\nNumele userului: " $FullName "\nNumarul de procese pentru " $user ": " $NrProcese "\n"
   read -p "Dati user:(0 for over)" user
 done

2 个答案:

答案 0 :(得分:2)

您的user变量对于您尝试的数字比较几乎肯定无效。插入一行:

echo "[$user]"

while循环之前检查它。

如果希望能够处理非数字用户,则应使用基于字符串的!=而不是基于数字的-ne,以下成绩单:

pax> xyzzy=plugh

pax> if [ $xyzzy -ne "0" ] ; then echo yes ; fi
bash: [: plugh: integer expression expected

pax> if [ $xyzzy != "0" ] ; then echo yes ; fi
yes

来自bash联机帮助页(我的斜体):

  

string1!= string2

     

如果字符串不相等,则为真。

     

arg1 OP arg2:

     

OP是-eq,-ne,-lt,-le,-gt或-ge之一。

     

如果arg1分别等于,不等于,小于,小于或等于,大于或大于或等于arg2,则这些算术二元运算符返回true。 Arg1和arg2可能是正整数或负整数。

答案 1 :(得分:0)

试试这个:

while [[ $user -ne 0 ]]

注意:我假设你在那里传递了一个有效的数字 - 我的意思是user。如果您通过了ilir1477,那么它将无效,因为您的if语句正在比较数字,而不是字符串。