Shell script lecture des arguments
fifi
-
Ro&ri Messages postés 751 Statut Membre -
Ro&ri Messages postés 751 Statut Membre -
Bonjour,
J'ai fait un shell script qui permet de "killer" soit un user, soit un process, soit un process correspondant à un user.
quand je le lance avec les arguments suivants par example, pour "killer" le user jean et le process vi ca marche bien: sh kill_proc jean vi
Mais si je veux le lancer afin qu'il arrete plusieurs process à la fois (par example sh kill_proc jean vi|more), il n'arrete que le process vi et il ne prend pas en compte le process more. En fait concernant le 2ieme argument il ne lit que la partie qui est avant le |. Comment pourrais-je faire pour kil lise le vi et le more?
Merci
#!/usr/bin/ksh
##########################################################################################
#PROCEDURE :kill a usersame, or a process, or a process associated to a user
#kill_proc.ksh "username" #kill_proc.ksh "process" #kill_proc.ksh "username" "process"
##########################################################################################
#----------------
# Read the inputs
#----------------
INPUT1=$1
INPUT2=$2
NB_INPUTS=$#
USER_NAME=$(more /etc/passwd | grep "^$INPUT1")
#---------------------------
# Number of inputs incorrect
#---------------------------
if [ "$NB_INPUTS" != "1" ] && [ "$NB_INPUTS" != "2" ]; then
echo "You shoud precise a username, and/or a process to kill"
fi
#-----------------------------------------------------
#Check if the input is a username or a process to kill
#-----------------------------------------------------
if [ "$NB_INPUTS" == "1" ];then
#Input is the same user as we're using
if [ "$USER_NAME" != "" ] && [ "$INPUT1" == "$USER" ];then
ps -fu $INPUT1 | egrep -v 'ps|grep|kill' | awk 'NR != 1 {print $2}' > a.tmp
if [ 'cat a.tmp | wc -l' == "0" ];then
echo "There is no processes running for user $INPUT1"
else
echo "Killing the processes of user $INPUT1"
kill -9 'ps -fu $INPUT1 | egrep -v 'ps|grep|kill' | awk 'NR != 1 {print $2}''
fi
elif [ "$USER_NAME" != "" ] && [ "$INPUT1" != "$USER" ];then
echo "Permission Problem: you should run the script from the user $INPUT1"
#Input is a process
else
PROC=$1
ps -fu $USER | egrep -v 'ps|grep|sh kill' | grep $PROC | awk '{print $2}' > a.tmp
if [ 'cat a.tmp | wc -l' == "0" ];then
echo "Error:incorrect username/process"
else
echo "Killing the processes"
kill -9 'ps -fu $USER | egrep -v 'ps|grep|sh kill' | grep $PROC | awk '{print $2}''
fi
fi
#-------------------------------
#Check if the inputs are correct
#-------------------------------
elif [ "$NB_INPUTS" == "2" ];then
if [ "$USER_NAME" == "" ];then
echo "user not found: input1=user input2=process"
elif [ "$USER_NAME" != "" ] && [ "$INPUT1" != "$USER" ];then
echo "Permission Problem: you should run the script from the user $INPUT1"
else
ps -fu $INPUT1 | egrep '$INPUT2' | awk '{print $2}'
#ps -fu $INPUT1 | egrep -v 'ps|grep|sh kill' | grep $INPUT2 | awk '{print $2}' > a.tmp
if [ 'cat a.tmp | wc -l' == "0" ];then
echo "Incorrect process name for username $INPUT1"
else
echo "Killing the processes $INPUT2 for the user $INPUT1"
kill -9 'ps -fu $INPUT1 | egrep $INPUT2 | awk '{print $2}''
#kill -9 'ps -fu $INPUT1 | egrep -v 'ps|grep|sh kill' | grep $INPUT2 | awk '{print $2}''
fi
fi
fi
J'ai fait un shell script qui permet de "killer" soit un user, soit un process, soit un process correspondant à un user.
quand je le lance avec les arguments suivants par example, pour "killer" le user jean et le process vi ca marche bien: sh kill_proc jean vi
Mais si je veux le lancer afin qu'il arrete plusieurs process à la fois (par example sh kill_proc jean vi|more), il n'arrete que le process vi et il ne prend pas en compte le process more. En fait concernant le 2ieme argument il ne lit que la partie qui est avant le |. Comment pourrais-je faire pour kil lise le vi et le more?
Merci
#!/usr/bin/ksh
##########################################################################################
#PROCEDURE :kill a usersame, or a process, or a process associated to a user
#kill_proc.ksh "username" #kill_proc.ksh "process" #kill_proc.ksh "username" "process"
##########################################################################################
#----------------
# Read the inputs
#----------------
INPUT1=$1
INPUT2=$2
NB_INPUTS=$#
USER_NAME=$(more /etc/passwd | grep "^$INPUT1")
#---------------------------
# Number of inputs incorrect
#---------------------------
if [ "$NB_INPUTS" != "1" ] && [ "$NB_INPUTS" != "2" ]; then
echo "You shoud precise a username, and/or a process to kill"
fi
#-----------------------------------------------------
#Check if the input is a username or a process to kill
#-----------------------------------------------------
if [ "$NB_INPUTS" == "1" ];then
#Input is the same user as we're using
if [ "$USER_NAME" != "" ] && [ "$INPUT1" == "$USER" ];then
ps -fu $INPUT1 | egrep -v 'ps|grep|kill' | awk 'NR != 1 {print $2}' > a.tmp
if [ 'cat a.tmp | wc -l' == "0" ];then
echo "There is no processes running for user $INPUT1"
else
echo "Killing the processes of user $INPUT1"
kill -9 'ps -fu $INPUT1 | egrep -v 'ps|grep|kill' | awk 'NR != 1 {print $2}''
fi
elif [ "$USER_NAME" != "" ] && [ "$INPUT1" != "$USER" ];then
echo "Permission Problem: you should run the script from the user $INPUT1"
#Input is a process
else
PROC=$1
ps -fu $USER | egrep -v 'ps|grep|sh kill' | grep $PROC | awk '{print $2}' > a.tmp
if [ 'cat a.tmp | wc -l' == "0" ];then
echo "Error:incorrect username/process"
else
echo "Killing the processes"
kill -9 'ps -fu $USER | egrep -v 'ps|grep|sh kill' | grep $PROC | awk '{print $2}''
fi
fi
#-------------------------------
#Check if the inputs are correct
#-------------------------------
elif [ "$NB_INPUTS" == "2" ];then
if [ "$USER_NAME" == "" ];then
echo "user not found: input1=user input2=process"
elif [ "$USER_NAME" != "" ] && [ "$INPUT1" != "$USER" ];then
echo "Permission Problem: you should run the script from the user $INPUT1"
else
ps -fu $INPUT1 | egrep '$INPUT2' | awk '{print $2}'
#ps -fu $INPUT1 | egrep -v 'ps|grep|sh kill' | grep $INPUT2 | awk '{print $2}' > a.tmp
if [ 'cat a.tmp | wc -l' == "0" ];then
echo "Incorrect process name for username $INPUT1"
else
echo "Killing the processes $INPUT2 for the user $INPUT1"
kill -9 'ps -fu $INPUT1 | egrep $INPUT2 | awk '{print $2}''
#kill -9 'ps -fu $INPUT1 | egrep -v 'ps|grep|sh kill' | grep $INPUT2 | awk '{print $2}''
fi
fi
fi
A voir également:
- Shell script lecture des arguments
- Classic shell - Télécharger - Personnalisation
- Script vidéo youtube - Guide
- Lecture epub sur pc - Guide
- Confirmation de lecture whatsapp - Guide
- Accusé de lecture gmail - Guide
4 réponses
Salut,
Peut être devrais-tu t'orienter vers la commande "getopts" pour gérer tes différents paramètres, non ?
Exemple d'utilisation...
Peut être devrais-tu t'orienter vers la commande "getopts" pour gérer tes différents paramètres, non ?
Exemple d'utilisation...
Bon un exemple plus concret se rapprochant de ce que tu veux faire, mais il faudra approfondir et adapter ;-))
;-))
$ cat foo.sh
#! /bin/bash
#set -xv
while getopts ":u:p:" OPTION
do
case $OPTION in
u) FLAG=1
grep -q "$OPTARG" /etc/passwd
RETVAL="$?"
if [ "$RETVAL" != 0 ]
then
echo "Le paramètre \"-u\" nécessite un nom d'utilisateur existant."
echo
exit 3
else
echo "Utilisateur : $OPTARG"
fi
;;
p) if [ "${FLAG}" = 1 ]
then shift 3
else shift 1
fi
for PARAM in "$@"
do
echo "Kill -9 $PARAM"
done
;;
:) echo "Argument manquant pour l'option -$OPTARG"
read -s -n1 -p "Appuyez sur une touche..." touche
echo
exit 4
;;
\?) echo "Option inconnue \"-$OPTARG\" ! "
read -s -n1 -p "Appuyez sur une touche..." touche
echo
exit 5
;;
esac
done
shift $((OPTIND-1))
$ ./foo.sh -u zipe -p toto tata titi
Utilisateur : zipe
Kill -9 toto
Kill -9 tata
Kill -9 titi
$ ./foo.sh -u -p toto tata titi
grep : option invalide -- 'p'
Usage: grep [OPTION]... MOTIF [FICHIER]...
Pour en savoir davantage, faites: « grep --help ».
Le paramètre "-u" nécessite un nom d'utilisateur existant.
$ ./foo.sh -u zipe -p
Utilisateur : zipe
Argument manquant pour l'option -p
Appuyez sur une touche...
$ ./foo.sh -u toto -p toto tata titi
Le paramètre "-u" nécessite un nom d'utilisateur existant.
$
;-))