Scrpt tcsh problème variable

geekface Messages postés 29 Date d'inscription   Statut Membre Dernière intervention   -  
geekface Messages postés 29 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour à tous,

Je m'amuse à faire un petit script, mais je rencontre un petit problème. Voila ce que je veux :

- créer un script "prog" qui lorsqu'il est lancé avec un paramètre le nom d'un utilisateur celui renvoie ceci :

./prog nomlogin

uid : 1890

gid : 568

fullname : Nom Prénom

homedir : /home/nomlogin

shell : /bin/tcsh

voila ce que j'ai fait :

#!/bin/tcsh

grep $1 /etc/passwd

endif

il m'affiche seulement la ligne correspond au nom de login indiqué au prélable en paramètre du fichier /etc/passwd

Moi je veux qu'il affiche en colonne, vous savez avec la commande awk'{print $1 "" $3}', pour afficher les colonnes 1 et 3 du fichier, mais bon voila quoi il ne veut rien savoir même si je met des pipes (|).

Je voudrais après l'exécution de la commande :

./prog jean
ou
./prog "nom d'un utilisateur (ex: root, marcel, pascal ...)"

celle ci m'affiche :

uid : 1890

gid : 568

fullname : dupond jean

homedir : /home/jean

shell : /bin/tcsh

car dans le fichier :

/etc/passwod:

on a :

jean:x:1890:568:dupont jean:/home/jean:/bin/tcsh
marcel:x:1111:564:dupuit marcel:/home/marcel:/bin/bash
autres utilisateurs ...
....
....

si j'avais tapé la commande :

./prog marcel

Alors elle m'aurais affiché :

uid : 1111

gid : 564

fullname : dupuit marcel

homedir : /home/marcel

shell : /bin/bash

Voila ce que j'ai fais :

VERSIONS QUI MARCHE avec uid gid

#!/bin/tcsh

if ($1 == "-help") then

echo "Usage: infouser [-help] username"

echo "Display user information (uid,gid,full name, homedir and common shell)"

else

echo uid:$uid

set uid = "grep $1 /etc/passwd | awk -F : '{print $3}'"

echo gid:$gid

set gid = "grep $1 /etc/passwd | awk -F : '{print $4}'"

endif

VERSIONS QUI MARCHE PLUS :

#!/bin/tcsh

if ($1 == "-help") then

echo "Usage: infouser [-help] username"

echo "Display user information (uid,gid,full name, homedir and common shell)"

else

echo uid:$uid

set uid = "grep $1 /etc/passwd | awk -F : '{print $3}'"

echo gid:$gid

set gid = "grep $1 /etc/passwd | awk -F : '{print $4}'"

echo fullname:$fullname

set fullname = "grep $1 /etc/passwd | awk -F : '{print $5}'"

echo homedir:$homedir

set homedir = "grep $1 /etc/passwd | awk -F : '{print $6}'"

echo shell:$shell

set shell = "grep $1 /etc/passwd | awk -F : '{print $7}'"

endif

Pouvez vous m'aidez MERCI
Configuration: Windows XP pro sp3
Firefox 3.5.4

4 réponses

  1. jipicy Messages postés 40842 Date d'inscription   Statut Modérateur Dernière intervention   4 898
     
    Salut,

    Juste un truc, mets les "echo $uid $gid $fulname etc." après leur déclaration et non pas avant ;-(
    0
  2. geekface Messages postés 29 Date d'inscription   Statut Membre Dernière intervention   1
     
    J'ai essayer ça ne change rien, d'autre supposition

    Merci
    0
  3. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
     
    hello

    $ cat prog
    #!/bin/bash
    
    test -z "$1" && { echo "Usage: $0 <nom>" ; exit ; }
    awk -F: '/'"$1"'/ {printf("uid: %s\ngid: %s\nfullname: %s\nhomedir: %s\nshell: %s\n",$3,$4, $5, $6,$7)}' </etc/passwd
    $ 
    $ ./prog root
    uid: 0
    gid: 0
    fullname: root
    homedir: /root
    shell: /bin/bash
    $ 
    $ ./prog 
    Usage: ./prog <nom>
    
    0
    1. hamix5
       
      Ok c'est cool merci.

      Mais j'insiste sur mon script en tcsh pouvez vous le corriger en restant dans le même état d'esprit. Merci infiniment.

      A bientôt
      0
  4. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
     
    et comme ça:
    host:~/tcsh> cat prog
    #!/bin/tcsh
    
    if ($1 == "-help") then
    
    echo "Usage: infouser [-help] username"
    echo "Display user information (uid,gid,full name, homedir and common shell)"
    
    else
    
    set uid = `grep $1 /etc/passwd | awk -F : '{print $3}'`
    echo uid:$uid
    
    set gid = `grep $1 /etc/passwd | awk -F : '{print $4}'`
    echo gid:$gid
    
    set fullname = `grep $1 /etc/passwd | awk -F : '{print $5}'`
    echo fullname:$fullname
    
    set homedir = `grep $1 /etc/passwd | awk -F : '{print $6}'`
    echo homedir:$homedir
    
    set shell = `grep $1 /etc/passwd | awk -F : '{print $7}'`
    echo shell:$shell
    
    endif
    
    host:~/tcsh> ./prog -help
    Usage: infouser [-help] username
    Display user information (uid,gid,full name, homedir and common shell)
    
    host:~/tcsh> ./prog root
    uid:0
    gid:0
    fullname:root
    homedir:/root
    shell:/bin/bash 
    
    0
    1. geekface Messages postés 29 Date d'inscription   Statut Membre Dernière intervention   1
       
      WHAOOOOOOOOO, c'est super franchement merci beaucoup, syntaxe pourrie sur tcsh, mais bon fallait y penser avec les ``````````.

      Merci infiniment.
      0