Scrpt tcsh problème variable
geekface
Messages postés
31
Statut
Membre
-
geekface Messages postés 31 Statut Membre -
geekface Messages postés 31 Statut Membre -
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
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
A voir également:
- Scrpt tcsh problème variable
- Vba range avec variable ✓ - Forum VB / VBA
- Variable d'environnement temp ✓ - Forum Microsoft Office
- Variable objet ou variable de bloc with non définie - Forum VB / VBA
- Variable objet ou bloc "with" non définie - Forum VB / VBA
- Impossible de créer le fichier de travail. vérifiez la variable d'environnement temp - Forum Microsoft Office
4 réponses
Salut,
Juste un truc, mets les "echo $uid $gid $fulname etc." après leur déclaration et non pas avant ;-(
Juste un truc, mets les "echo $uid $gid $fulname etc." après leur déclaration et non pas avant ;-(
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>
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