Csh issue with Illegal variable name

那年仲夏 提交于 2019-12-11 02:13:08

问题


Ok that's my whole code I have a new Error: Illegal variable name.

When I execute the file with: csh filename.sh

Result is: Illegal variable name.

I think that the problem containing in the part of: while ( $? == 1 ) #!/bin/sh

set quitter = "N"
# Boucle sur  la condition d'arret du script:
while ( $quitter == "N" )

 # Saisie du nom de l'utilisateur :
 echo "Quel utilisateur ?"
 set a = $<
 # Mettre le résultat de la commande ps -u 
 # dans un fichier quelque soit le résultat (juste ou faux) :
 ps -u $a >&fichier
 # La varible $? vaudra 1 si la dernière commande qui a été éxcuter 
 # a retourné une erreur, 0 sinon.
 # On boucle donc j'usqu'a ce que le nom d'utilisateur soit correct:
 while ( $? == 1 )

   echo -n "Nom d'utilisateur innexistant, entrez un autre :"
   set a = $<
   ps -u $a >&fichier

commande=$(tail -$i tempfile|head -1|cut -d" " -f2)
let i=i+1
echo -n " $commande : "
case $etat
in
D) echo "endormi => ininterruptible" 
S) echo "endormi" 
R) echo "en cours" 
T) echo "stoppe" 
Z) echo "zombi" 
*) echo "inconnu" 
esac
end
 # Suppression du fichier qui a servi aux tests
 rm fichier;
 echo -n "voulez-vous quitter ? (O/N):";set quitter = $<
end

回答1:


Your code seems to be a mix of bash and csh AND by your initial opening (the shebang), you have #!/bin/sh, which is definitely not csh, and is ambiguous as the old-line sh=originaUnix*Bourne*Shell OR a linux system may have a link to /bin/bash as /bin/sh for convenience sake.

The error message is telling your the right thing, $? is not a valid variable in csh, you want to use the equivalent $status.

I don't have access to a csh to run your code, but I see several things I question and some I know are wrong for csh syntax.

commande=$(tail -$i tempfile|head -1|cut -d" " -f2)

won't work, try

set commande = `tail -$i tempfile|head -1|cut -d" " -f2`
# tnx to @dwalter for correct syntax on that!

csh case sytnax is

 switch ( "$etat" )
      case D:
        echo "endormi => ininterruptible" 
      breaksw
     default:   
         echo "unknown option provided in as etat=$etat"
     breaksw
  endswitch

and, I'm skeptical about

ps -u $a >&fichier

What is your intent there? If you're writing to the file fichier, I don't see where you're reading from it. What purpose does it serve?

If you need further help with this, please edit your question to include the exact error message (formatted as code).

P.S.

If you require to use Unix scripting in the future, you're overall marketability will be enhanced by dropping use of csh and converting to ksh/bash. As much of the code is bash, you'd be better served changing the first line to #!/bin/bash and then researching and fixing the resulting error messages.

IHTH




回答2:


First of all change #!/bin/sh to #!/bin/csh (just for completeness).

Then change

commande=$(tail -$i tempfile | head -n 1|cut -d" " -f2)

to

set commande = `tail -$i tempfile | head -n 1|cut -d" " -f2`

also your let i=i+1 should be set i = 0 at the top and after wards use @ i++ to increment it.

The next error is that $etat is not defined.

Also Csh uses switch() case.

The best thing would be you have a look at http://www.grymoire.com/Unix/Csh.html or http://faculty.plattsburgh.edu/jan.plaza/computing/help/tcsh.htm for more information on csh scripting.



来源:https://stackoverflow.com/questions/13950960/csh-issue-with-illegal-variable-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!