Programmation Shell
Transcription
Programmation Shell
!" !" # &' $ % $$ !" # # ## $ $ '* # ' $ !( $ & ) + ' $ % "" & & '' '' ' ! ! ) ) $ ) #$ * " & " , " " . $" $ '' ! " " ) / 0 0 $ % + -" - 12 $ 13 - 1 - # 4% $ -& $ -) ) & , - ' $ ' " ( & & # ) , / . / 0 0 0 1 ! 5' ' ! % 6 # 123 $ 7;7< 8" => "? 7 ? ! 7 : > C ? >C ? 8 : 8 7 7:" 7 ?77 ? 8 > > ? 8 : C: 8 79: 78 : 8 7@ >> C7 ?77 7: > @ 7 =C:? :> 8" D? 8 ?A 7 : 8 7 ?= : 87 =7 7 ?8" ? 7 8 7> :C8? 7 @ 7 8" A?B? 9: =7 >?7 ? 7 ;> 78 ? 2EF E7 3 2E F E:7 3 G 2EF EH7 3 • • • $ ?= > > 7 8?G 4 :C 53 ? 2EF EF 7 3 B? C? 8 77>C:9: C78? ? 1 2 63 7 8"? 7: > 7: > 7 ? 7 F 8 : 8 7> :C 78 7? 9: : 9: : 7> 8 I ? B?(? >C ? ? >? > C ? B? @ ;7 =C:? 7: 8 7B?( : @ ( >C ? 7 ? 8 7 78( =27 @ :7 @ H7 3 = B? 8 7: > 7 8 7 CI ? ! 7 : 78(? 7: > $ > < 8(? 7: > 8 7>C:9 > I >> >? ( >C 8 : : > 7CB? : 89 7?A 8( ? 7 ? 7> : >?7 8? 8 ( >C ? ? ? 7: > C: ? @ 7 89:; : 6 $ ?= > >7 ? EF E7 7 ? 7? EF EF 7 ? ? B? 7 8 > F C@ 7 : 7 C8"? 7 7 $ • • 8(? 7: > > ? J B? : B? 7 : 7 CC 7 K L7: >K27 7 = 7 3@ > ? B? 7 ( 8(? : : ? ? @ 7 K L7: > 7 K2 ?K L7: > :7 K7( C C: 3@ > ? : B?( 7( 8(? 7: > A: • • 8 = 8B? 8"? 7: > 7: > > ? J A B?C> 7: 8 77?A 7 7 L7: > B? A : ? ?A ? 27 ?7 L7: >@ 7 ?: L7: > 2 ? L7: >3 B? =C:? 9: :? B? > ? 8 : C 7 A : 3 =C:? 9: L7: > 8 7 8 : A ::8@ • :C ? 89: 8 A F 7@ : @ $ 7; = 8( =C:? L7: > B? 7 ?7> E L7: > B? : ? ?A ? =C:? 9: ? : 8 < C 8@ : : 2? 7 8 : =C:? : 9: 3 L7: > > ? L7: > 9?8 B? 7 8 7 8( =C:? 7 >7 C7 8 : 8?M= L7: > > ? B? "? 7 ? >?77 = > ! 7 " # $$% & '( )*+ ,,,., ,*/0 1//.2+ &34 & & && 54$ & & && 546 & 7 & && 89 7 ?7 : 8 >7 =C:?C 8 7 7 ?7 " # $$% & '(: )*+ ,,,., ,*/0 1//.2+ &34 & & && 545 & & && : 544 & 7 & && 89 7 ?7 : 8 >7 =C:?C 8 7 7 ?7 " # $$% & '( )*+ ,,,., ,*/0 1//.2+ &34 & & && 54; & 7 & && : 8 >7 =C:?C 8 7 8 7 ?7 " " 2 "; > 7 N3 # $$% & '( ! # $$% & '( (<1/0 " # $$% & '(: )*+ ,,,., ,*/0 1//.2+ &34 & & && 543 & 7 & && " # $$% 99: A & '( ! @ 8 7 78 ?=8 7: 7@ # 7: > : C" A 8? #A ? ? C > ? 7: > ( 7 A ? ? C> 8 < : A ? > A B? 9 8?7: > > : ? C >? ? : 8@ A ? 8 7 A F OP2 : OP3 : A @? 7: > 7( 7 F 8C ?C A 7 @ ? ? A 8 9 ? ? A ?> 9: 7: > : > 8 7A 7 : 8 7" 7 : 7 ? 8 7CC 78 > 7 > Q : & 7 ?> $ 7 7: >7 8"J > B? > ? =C:? 8 : 8 A: 7 > 2 > :? 7A > T : 9?7 ? A= L7: > 2 ? : 7> < ; 6 7 # 8 =C:?C > 7: > > ? 9= 8 = ! 7: : 7@ :( 7 B? 7 8 ? 8(? 7: > > ? J C:?>CC A ?8 ?C I 0 7 9 B? ? 8B? ? ? 7 : 8C ? 7 : 7: > 8 7? ? 7: >@ : 8C ?C ? $ C? 7 A 7( =C:? 7 7 99: 7: 8 7 =C:?C 7@ : B? 7 7 ?> >? : : B? 7 ? 78 7; = 7 7 R: I " > S >? A 8C 8 ( =C:? A B? 7 A ? A= $( 99: A: ( > A= 7 >?7: > B?( A : A F 77 > :C 7> ? A ?3 7 C7? I (C: > ? 7 ?> @ > ? 8 : : ? 7: > > : 8 7 ( >> : A @> 7 @ 7 > <7 A > 77 8 UNEF E 7 SA=3 < 6 F 77 > 7 A F 7 > 8C9? ? : T 2 J 7 : ? A ? ? C B? 3 ? @ 8C9 ? A F 7 9 > : 8 A F VKA ? K 2 ? A F V(A ? (3 A ? : > ?:? F :@ >? 7 ? 72 ? 7 > 7 > 73 7 > B?( (; 8( 7> : 7 A > <7 7 V A ? 7 F7 @ A F 7 :CC 7: ? : T A8 $(? 7 8(? A F 7 9 R: I ( => 77 OA F ? OWA F X $ 8 ?=< 7 C: 77 7 A F 7 7?A 8(? = B? > ? J > CC: 97 > 8? 8 A F = > 7 [prof1@228-poste10 prof1]$ Var="texte1" [prof1@228-poste10 prof1]$ echo $Var texte1 [prof1@228-poste10 prof1]$ echo "$Var" texte1 [prof1@228-poste10 prof1]$ Prix=50 [prof1@228-poste10 prof1]$ echo "Cet article vaut $PrixE" Cet article vaut (la variable PrixE n'existe pas) [prof1@228-poste10 prof1]$ echo "Cet article vaut ${Prix}E" Cet article vaut 50E (les accolades permettent d'isoler le nom de la variable) [prof1@228-poste10 prof1]$ Var="texte2" [prof1@228-poste10 prof1]$ echo $Var texte2 [prof1@228-poste10 prof1]$ echo ${Var} & texte2 [prof1@228-poste10 shell]$ echo "$Var" texte2 [prof1@228-poste10 prof1]$ echo '$Var' $Var [prof1@228-poste10 prof1]$ echo `$Var` bash: texte2: command not found [prof1@228-poste10 prof1]$ Var="ls -l" [prof1@228-poste10 prof1]$ echo "$Var" ls -l [prof1@228-poste10 prof1]$ echo `$Var` total 2 drwxr-xr-x 3 prof1 prof1 1024 jun 2 13:43 Desktop drwx------ 2 prof1 prof1 1024 jun 2 13:42 tmp[prof1@228-poste10 prof1]$ unset Var $ 89: 8(? A F 7 > ( =7 B? 8 7 7: > > ? 8 : D 7 89 7 A F 7 7 > 7 8? >> C A : : 8 62 3 [prof1@228-poste10 shell]$ #! /bin/sh echo $Var Var="contenu de exemple1" echo $Var ./exemple2 [prof1@228-poste10 shell]$ #! /bin/sh echo $Var [prof1@228-poste10 shell]$ [prof1@228-poste10 shell]$ Texte3 [prof1@228-poste10 shell]$ B? ( 8C: C : ? @ 7 ?97( 7 cat exemple1 cat exemple2 Var=Texte3 echo $Var ./exemple1 contenu de exemple1 [prof1@228-poste10 shell]$ echo $Var Texte3 [prof1@228-poste10 shell]$ source exemple1 Texte3 contenu de exemple1 [prof1@228-poste10 shell]$ echo $Var contenu de exemple1 # A F 78( A ? : 8? @ F ?: ?> 8 A F 7 7 7C 7 7 >> 7 A F 78( A CC 77 C: 7 D?7:? 72 9 8 787 ? 8 7A F 77 > 7B? 7 @ CC @ ?7:? 73 = > @ : 8 (? 7 ? @ % ' : : 8 7 C> > 7 @ : ? A 7 : >< @ 9? ? 7 : 8 =7 >? 79 ? A F 7 > A F 8( A R: I : 8 =7 7?A 8? 8 A F 7 > 77F 8 8C9 ? A F 8( A > : 8 =7 5 ; >5 2 ? 99: 7 : >< 8 7A F 727 > 7 8( A 3 9? ? 7 : 8 6 $ : 8 26 5 ; > 8 8C ? 8C9 A A F ) ?7 ? @: 7A F 7 ? 7 77>C: • OO 8 ? C 8 > : 77?72>83 8? @ • ON8 ? C 8 > : 77?72>83 8 8 < : 8 9 82:( 7 I 8 A : ( >C ? (Y(3@ • OP8 A ? ? C> 8 < : 8@ • O 8 7 8 7 > 7 A : 7B? 7 CC >> C [prof1@228-poste10 shell]$ export Var [prof1@228-poste10 shell]$ echo $Var contenu de exemple1 [prof1@228-poste10 shell]$ Var=Texte4 [prof1@228-poste10 shell]$ echo $Var Texte4 [prof1@228-poste10 shell]$ ./exemple1 Texte4 contenu de exemple1 contenu de exemple1 [prof1@228-poste10 shell]$ echo $Var Texte4 2 >< A >7 7 [prof1@228-poste10 prof1]$ unset Prix [prof1@228-poste10 prof1]$ unset Var [prof1@228-poste10 prof1]$ echo "$Prix" 2 A F 89: 78 7 :C R: 8 79 73 = ( =7 >?73 [prof1@228-poste10 shell]$ ps f PID TTY STAT TIME COMMAND 3411 pts/1 S 0:00 /bin/bash 3434 pts/1 R 0:00 ps f [prof1@228-poste10 shell]$ echo $$ 3411 2: 9 :( 7 F ! 8? :? 3 [prof1@228-poste10 shell]$ echo $? 0 2 8 < : 8 7( 7 F 8C ?C 3 [prof1@228-poste10 shell]$ ls -l a* ls: a*: Aucun fichier ou répertoire de ce type [prof1@228-poste10 shell]$ echo $? 1 2 8 < : 8 C: ?C 3 # #> < 7 7: > 7 A B?C A :8 7> < 7@ 8 7A F 77?>>C 77 :CC 7 $ A F OU 8 F 8 > < 7 :: > ( >> 8? 7: > $ 7 A F 7OZ O[ 8 ( 7 F 8 7> < 7 > ? ::C8 I : B? > < 7>C:9 7 ? C > <7 7 O ? C 7? 8 ?=: 99 7 ?>?7@ 8 J 8 C :: 8 7 O @ @ O/@ OW 0X@ OW X $ > < O0: 7> 8 ? 8? 7: > $ : 8 6 ? C 7 > 7> < 78 7 8 7> < 72 8 > 7J 7?>C ? ? F 8 > < 73 $ A F OU 7 8 ?C 8( ? ( 7 > 78 C@ 7 7?>> 7C C I > <7? : 8 6 ?@ 7> < 77 8 : 8C: C7@ :( 7 I 8 B? O > 8 A ? 8 O2 M 3@ O > 8 A ? 8 O2 M 3@ : + = > [prof1@228-poste10 shell]$ cat param #! /bin/sh echo "Le nom du script est $0" echo "Il y a $# paramètres" echo "Les paramètres sont les suivants : $@" echo "Premier paramètre : $1" echo "Second paramètre : $2" echo "Cinquième paramètre : $5" shift 2 echo "Après commande shift 2" echo "Premier paramètre : $1" echo "Second paramètre : $2" echo "Troisième paramètre : $3" [prof1@228-poste10 shell]$ ./param exemple passage paramètres 1 50 Le nom du script est ./param Il y a 5 paramètres Les paramètres sont les suivants : exemple passage paramètres 1 50 Premier paramètre : exemple Second paramètre : passage Cinquième paramètre : 50 Après commande shift 2 Premier paramètre : paramètres Second paramètre : 1 Troisième paramètre : 50 $ & 7; 1 61 2 63 7 : 8 77 > 7 ! 7 ? 7: >@ >? > ; ? : 8 =C:? F > (? 7 ? 7 ?9 7 ; 7:CC7 A : 7 • ? :C >> ?! 2 @ 2 = > 9@ : 7 @ 3@ • ? : 8 2F? 3 ?! 2 @ 2 = > : @ ? 7H@ 3@ • ? 9 : 8C9 > (? 7 ? A : :C9? : @ • ? : 8 = 2: 8 >> ?=: 78 A F %3 ? 7 A 8( \> A ? : 8@ >? ? 7 : 8 ;> " ! " # $$% & > =! # $$% & '( = ! " # $$% '( = A B '( = > " > & ? @ # $$% & > '( = ? : B? 7? ? : :< 8 7 : 78CC: ? : ? : 8 7?A 8?: :< (Y( 7( =C:? R: 8 9 82 < > 3 : 8 > ? 7(C: 7? >?7 ? 7 7 78 7: : 7@ ? 7 7 77 ?9 8 < 8 A J C7 > A > ? 8B? B?( 9? B? 7? >? : T 7: 8 77? ? J I: 8 8 77C> > : :< B , & : 8 7: > 7C 7 $ 7 = > 7B? 7?A >?78 : 87 • 7CB? : 3 7 8 C7 A :8 ?=: 1 B3 87 7> ?A J C 8?7I 1 =C:? : 8 >?7: 8 ?= 7 ?: 7B? 8 A CA >?7B?( ? I 7F C • ?F 3 1 C3 1 8 J $ > A ? > 8 C? 7? ? 7 ? 7? 8 ?= 77C> C 7 ? 7 7I =C:? : 8 : 8 5 R: ? ?F @ : 8 99:? 7 7? = B?( ? 99: C : 8 @ : 8 : 8 7( =C:? 8 7 8 ?= 7 7 : 8 7 27 ?7 73 @ > : 7CB? @ ? 89: B?( 7 >> I ( A 2A F 7@ : :? @ 3 7 > 8? 8<7 9 8 : 8 • > < 3 =C:? : 9 8 • 7? 8 ? 3 =C:? : 8 7 7: 8 • =C:? CC : 2 • > D3 : 1 8 1 C C3 > < : 3 1 8 R: 8 DD 3 8 C: ?C2 7 C C ? 7C 3 1 : 8 7 7? 7 : 8 ? 7C8 7 7: 8 78 77 ?:? 7 9@ ] < E3 7 =C:?C 1 8 7 7? 7: 78 77 ?:? 7 9@ ] ? 7? C?77 • 1 1 H ?3 1 FG3 7( 7 F ? 3 1 I3 8C ?C 2 7 1 I : F 7 =C:? : 8 A: > < = B?( ? 99: C 8 $ 7 8 ?= A 7 > > 7C 7 7 7 : 8 B? 7 ? 7 O23 > : B? ( B? 2^3 ( 7 > 7 ?D? 729: 3 :: 77F ? : A 5 ,3 ?> : B?( ( 7 > 7 <7899C F 8 ( > 7 > I (C: CA ? A E 5 FG2 J3 =C:? : 1H 8 B? ? 7 8 C > < = > 7 [prof1@228-poste10 shell]$ cat com_composees #! /bin/sh # Exemples de commandes composées echo "séquence : " echo $USER;pwd echo echo "tube : (comptage du nombre de ligne fourni par ls -l (donc nombre de fichiers + 1)" ls -l | wc -l echo echo "sur erreur : " ls -l fichier_manquant || echo "fichier_manquant n'existe pas" ls -l com_composees || echo "com_composees n'existe pas" echo - echo "sur réussite : " ls -l fichier_manquant && echo "fichier_manquant existe" ls -l com_composees && echo "com_composees existe" echo echo "en paramètre : " date heure=$(date +%H) minute=$(date +%M) echo "Il est ${heure}:${minute}:$(date +%S)" echo echo "évaluation : " commande="ls -l" eval $commande [prof1@228-poste10 shell]$ ./com_composees séquence : prof1 /home/prof1/shell tube : (comptage du nombre de ligne fourni par ls -l (donc nombre de fichiers + 1) 9 sur erreur : ls: fichier_manquant: Aucun fichier ou répertoire de ce type fichier_manquant n'existe pas -rwxr--r-1 prof1 prof1 646 jun 3 22:41 com_composees sur réussite : ls: fichier_manquant: Aucun fichier ou répertoire de ce type -rwxr--r-1 prof1 prof1 646 jun 3 22:41 com_composees com_composees existe en paramètre : dim jun 3 22:43:46 EDT 2001 Il est 22:43:46 évaluation : total 8 -rwxr--r-1 prof1 prof1 -rwxr--r-1 prof1 prof1 -rwxr--r-1 prof1 prof1 -rwxr--r-1 prof1 prof1 -rwxr--r-1 prof1 prof1 -rwxr--r-1 prof1 prof1 -rwxr--r-1 prof1 prof1 -rwxr--r-1 prof1 prof1 [prof1@228-poste10 shell]$ ' ) 82 165 111 646 590 70 23 368 jun jun jun jun jun jun jun jun 2 2 2 3 3 2 2 2 16:07 15:41 16:17 22:41 22:36 14:24 14:23 15:24 Compress Identite Run com_composees com_composees~ exemple1 exemple2 param 2 53 2 6 2 C: ? I "C: $ : 8 3 > 8(C: ? ? B? I 9 8? = $( > > (_ (@ (_ (@ : 2A 8 : 3 = I (C: $( > CA 7? 8 ( >C 8 7: :< 77>C: ?= (_ (@ / ) :? ?: A $ : 8 1 ? 7C 7 ? > 7 7 :HC 8 7 A F $` $ : ? > 7 ?: A 77 > 7 99:CI 7 8 > 7 7 = > 8 ? > 7 : >< $ > 7 ? 8 1 6 > ?77 8 7 99:CI A F @ 7 8 ?=< 99:CI A F 7 [prof1@228-poste10 shell]$ echo $REPLY [prof1@228-poste10 shell]$ read lecture sur plusieurs mots [prof1@228-poste10 shell]$ echo $REPLY lecture sur plusieurs mots [prof1@228-poste10 shell]$ read mot1 mot2 reste lecture avec plusieurs variables [prof1@228-poste10 shell]$ echo $REPLY lecture sur plusieurs mots [prof1@228-poste10 shell]$ echo $mot1 lecture [prof1@228-poste10 shell]$ echo $mot2 avec [prof1@228-poste10 shell]$ echo $reste plusieurs variables [prof1@228-poste10 shell]$ )# : $ : 8 7 : 8 6 3> 8( 99 ? ?I (? 7 ? 7; = 7 7 :A F K: = KK: = K K: = K 8 a 7 8( 7 ?: 7b 8 $ ?> C7 C: > ( 7 F 8 7: => > 7C7> C:C8C78(? ? C A 7 99: C 8? C ? : A $ 7B? (? 7 ? C7 : =@ : >78 F ?: 7 =C:?C $ A F $` : 7: B? (? 7 ? C2 ? ? C @ c 3 : = 8 (? 7 ? : 7> 8 I (? 8 7 > > 7 7 B? ? 7 9 7@ A F KA F K : ?77 : T : 7> 8 2K: = K@ K: = K@ ?K: = K3 = > [josephneveu@ServeurLinux ExemplesCoursShell]$ cat select.sh #! /bin/sh #exemple de la fonction select PS3="Entrez votre choix : " select variable in "choix1" "choix2" "choix3" "choix4" "Quitter" do if [ "$REPLY" -eq 5 ]; then echo -e "${REPLY}\t${variable}" break else echo -e "${REPLY}\t${variable}" fi done 0 [josephneveu@ServeurLinux ExemplesCoursShell]$ ./select.sh 1) choix1 2) choix2 3) choix3 4) choix4 5) Quitter Entrez votre choix : 1 1 choix1 Entrez votre choix : 3 3 choix3 Entrez votre choix : 6 6 Entrez votre choix : a ./select.sh: line 8: [: a: integer expression expected a Entrez votre choix : 5 5 Quitter [josephneveu@ServeurLinux ExemplesCoursShell]$ * 32 4 K2 $ > 8( 99:? 8 7: :?7 ; 8 ?=7; = 7> 77F 7 Od => 77 O22 => 77 C C C B? 7 7? B? 7? 8 7 7 B? e B? 33 $( => 77 C B? > ? 9 A ( > B? A F 2> 79 :C > C:C8C 8? 7 O3 7 B? ( > B? F 8C: 2 /3@ = 8C: 20=! ? +U! 3@ : 20### ? -U###3 ? F 2 U0 0 0 3 ?7 7 >C ? 78? 7 ? 7C72M@ @ [@ f@ g g @ V@ MV@ hV@ VV@ NV 3@ ;: > 7 7> <7 7 > ? ?77 ? 7 ! 7: : 7@ ( => 77 =C:?C = > 9 ? C K: B? 7 8 Od => 77 C B? eK > :C > 7 A ? >?7 : [josephneveu@serveurLinux Shell]$ cat calcul.sh #! /bin/sh #calculs mathématiques let $[ a=49 ] let $((b=11)) echo -e "a = ${a}\tb = $b" let $[ b += a ] echo -e "a = ${a}\tb = $b" let $[ a=0xAA ] echo "a = $a" let $[ b=057 ] echo "b = $b" let $[ a = a << 3 ] echo "a = $a" let $[ a=(a >> 2)&0xFF ] echo "a = $a" let $[ a = 2#01011100 ] 8 7 echo "a = $a" echo $((a|0xF0)) [prof1@228-poste10 shell]$ ./calcul.sh a = 49 b = 11 a = 49 b = 60 a = 170 b = 47 a = 1360 a = 84 a = 92 252 [prof1@228-poste10 shell]$ & " 63 1 6 ? 8 > 8 J :> F 8 7 8 7 : 8 7 $ 7 : 8 77 7C 7> ( C8 8 : 8 6 B? > ? J F C C > 9 ? d => 77 e $ 7 77 > 77F 7 > ?A > 7? 8 7 7@ 8 7 : T 7 8 : :< 7@ 8 79: 7 ?8 77: >78" \ " > :8 : 8 = 8 7 77: >7 6662 • • • • i: T A 7 : T A 7 : V: : NV : 6662 ; • • • • • • 6 6 ? 7: 7 B 6662 • • • • • • • • • • • • • • 63 L 6 6?3 9: 79: 9: ] 9: =9: 9: 5 9: F9: :9: 89: 99: 9: ?9: $ 9: : T : T A 7 A 7 > 78 7 A 7 A 7 A 7 A 7 A 7 A 7 6G 1 362 A A A A A A A A A A A A A A 7 A8 2j 3@ ( 7 > 7A8 2 i 3@ 78 ?=: T 77 8 B? 7@ 78 ?=: T 77 899C 7 7 ? ? 8 ( >C ? 7C I 2 B? 3@ 7 7?>C ? ?C I 77 : 7?>C ? I 7 9C ? ?C I 77 : 9C ? I 7 899C 8 2 6 47 2 ? B? 3@ 2 3@ 2 77 ? B? 3@ 2 77 3@ B? 3 6H 7 9: =7 @ 7 9: I ? ? @ 7 9: 7 7F @ 7 9: 7 89 F @ 7 9: 7 =C:? F ?7 C> 7 :: 77F @ 7 9: >> I (? 7 ? @ 7 9: >> ? ?> 8 (? 7 ? @ 7 9: 7 ? 9: 7>C: 8 ;> F :@ 7 9: 7 ? 9: 7>C: 8 ;> : :< @ 7 9: 7 ? C> @ 7 9: 7 ? 9: @ 77 ?> 8 7 : AC> ? : 9: @ 7 7 ?7 8 7 : AC> ? : 9: @ 7 9: 7? 7; F B? @ • >9: A F 8" ? 72A ; 6 1 7 9: 8 7? 7 ? ?F 7 73 C 66 $ : 8 B? 7 8 : 8 >? J ? : 8 : > 7C @ > :? ? 7 7 ?A ? 9 ? 8? ;> K: 8 YY : 8 K ? K: 8 g g : 8 K 2: 3 $ : 8 6 : T ? ? 7; = K: 8 S : 8 K2> ? 3 K: 8 S : 8 K2> ? 3 $ : 8 >? J A 7C 7 9 > C:C8 : 8 8(? > 8( =: ! : N: 8 2 ? 7 N: 8 3 7 9 B? : 8 7A 7 : 8 C: ? @ > 8( =: 7( >> B? ? B? I : 8 B? 7? k> ? B?( 7( >> B? I ? : 8 : > 7C @ 9? ( : 8 A : 8 7 :: 8 7 2 ? 8 7 > <7 73 , " 66 232 61 3 - 92 M 93 $ 7; = 8 : ?: 8 7 ?:? 8 : 7 ?: 273 7 ?: 273 l 7 7?A 6 ? $ > 7 7 > $ 7B? 7: > 8 ? C> 7 > 8 ?= > 77F 7@ >? J C 77 8 7 7 C> 7 7 99: A ? 8 78 ?= > 77F 7 ! 7: : 7@ >? ? 7 7 ?:? ? ?: 8 7 ?: ?: 8 273 2: 7 ? C 3 7 ?: 273 2: 7 ? C 3 7 ?: 273 2 6 "? " ? 3 ? # = > [josephneveu@serveurLinux Shell]$ cat signe.sh #! /bin/sh #détermine le signe du nombre saisi ou passé en paramètre if [ $# == 1 ] then let $[ nombre = $1 ] else echo -n "Entrez un nombre " read chaine if [ ${#chaine} -ne 0 ] then let $[ nombre = $chaine ] fi fi if [ ${#nombre} -ne 0 ] then echo "Nombre saisi : $nombre : " if [ $nombre -gt 0 ] then echo "Le nombre $nombre est positif !" elif [ $nombre -lt 0 ] then echo "Le nombre $nombre est négatif !" else echo "Le nombre $nombre est nul !" fi else echo "Vous n'avez pas saisi de nombre !" fi [josephneveu@serveurLinux Shell]$ Nombre saisi : 478 : Le nombre 478 est positif ! [josephneveu@serveurLinux Shell]$ Nombre saisi : -785 : Le nombre -785 est négatif ! [josephneveu@serveurLinux Shell]$ Nombre saisi : 0 : Le nombre 0 est nul ! [josephneveu@serveurLinux Shell]$ Entrez un nombre 78 Nombre saisi : 78 : Le nombre 78 est positif ! [josephneveu@serveurLinux Shell]$ Entrez un nombre -5698 Nombre saisi : -5698 : Le nombre -5698 est négatif ! [josephneveu@serveurLinux Shell]$ Entrez un nombre 0 Nombre saisi : 0 : Le nombre 0 est nul ! [josephneveu@serveurLinux Shell]$ Entrez un nombre Vous n'avez pas saisi de nombre ! [josephneveu@serveurLinux Shell]$ Nombre saisi : 0 : Le nombre 0 est nul ! ./signe.sh 478 ./signe.sh -785 ./signe.sh 0 ./signe.sh ./signe.sh ./signe.sh ./signe.sh ./signe.sh azerty & [josephneveu@serveurLinux Shell]$ ./signe.sh Entrez un nombre azerty Nombre saisi : 0 : Le nombre 0 est nul ! [josephneveu@serveurLinux Shell]$ - 9 $ 7; = 8 : 7 ?:? 8 : ? A F A ?7 1 7 ?: 273 1 l 7 7?A = > #! /bin/sh # boucle for chemin=$PWD cd /dev nb_fichier=0 nb_repertoire=0 nb_caractere=0 nb_bloc=0 nb_symbolique=0 nb_normaux=0 for fichier in * do nb_fichier=$[$nb_fichier+1] if [ -d $fichier ] then #fichier répertoire nb_repertoire=$[$nb_repertoire+1] elif [ -c $fichier ] then #fichier spécial de type caractère nb_caractere=$[$nb_caractere+1] elif [ -b $fichier ] then #fichier spécial de type bloc nb_bloc=$[$nb_bloc+1] elif [ -L $fichier ] then #lien symbolique nb_symbolique=$[nb_symbolique+1] else #fichier normal nb_normaux=$[nb_normaux+1] fi done cd $chemin echo -n "Dans le répertoire /dev il y a : $nb_fichier fichier(s)." echo -e "Parmi les $nb_fichier fichier(s) il y a : " echo -e "\t- $nb_repertoire répertoire(s)," echo -e "\t- $nb_caractere fichier(s) spécial(aux) de type caractère," echo -e "\t- $nb_bloc fichier(s) spécial(aux) de type bloc," echo -e "\t- $nb_symbolique lien(s) symbolique(s)," echo -e "\t- $nb_normaux fichier(s) normal(aux)." ) [prof1@228-poste10 shell]$ ./for.sh Dans le répertoire /dev il y a : 6589 fichier(s).Parmi fichier(s) il y a : - 12 répertoire(s), - 3672 fichier(s) spécial(aux) de type caractère, - 2899 fichier(s) spécial(aux) de type bloc, - 2 lien(s) symbolique(s), - 4 fichier(s) normal(aux). [prof1@228-poste10 shell]$ les 6589 = > #!/bin/sh echo "il y a $# paramètre(s) sur la ligne de commande" echo "liste des paramètres : $*" echo "le paramètre 1 est $1" echo le paramètre 20 est ${20}" i=1 for param in $* do echo "paramètre $i : $param" let $[ i += 1 ] done [prof1@228-poste10 shell]$ ./Nb_param_bis.sh a b c d ef gh ij kl mnop qrst 1 2 3 4 5 6 7 8 9 10 il y a 20 paramètre(s) sur la ligne de commande liste des paramètres : a b c d ef gh ij kl mnop qrst 1 2 3 4 5 6 7 8 9 10 le paramètre 1 est a echo le paramètre 20 est 10 paramètre 1 : a paramètre 2 : b paramètre 3 : c paramètre 4 : d paramètre 5 : ef paramètre 6 : gh paramètre 7 : ij paramètre 8 : kl paramètre 9 : mnop paramètre 10 : qrst paramètre 11 : 1 paramètre 12 : 2 paramètre 13 : 3 paramètre 14 : 4 paramètre 15 : 5 paramètre 16 : 6 paramètre 17 : 7 paramètre 18 : 8 paramètre 19 : 9 paramètre 20 : 10 A ?8 C > 8C9? ? : T @ F ?: ? ?> : ? 78(? 7 F 8 : T 7 7 ?< >> > C I ( =C:? ? F 8C C8 9 72: > = > 3 + 7F 8 : 8 >C 87 F ?: 8 0> 77 7 77 F I :: UNEF E7 9 #&)+,-/ 0 # & ) + , - / 0 8 7 ?: 273 8 - #] $ 7; = 8 : 7 ?:? 8 : N : 8 1 7 ?: 7 1 l 7 7?A = > #! /bin/sh # calcul d'une factorielle avec une boucle while if [ $# -ne 1 ] then echo "Usage : ./fact_while nombre" else if [ $1 -eq 0 ] || [ $1 -eq 1 ] then echo "La factorielle de $1 est 1" else i=1 resultat=1 while [ $i -lt $1 ] do let $[ i += 1 ] let $[ resultat *= i ] done echo "La factorielle de $1 est $resultat" fi fi [prof1@228-poste10 shell]$ ./fact_while La factorielle de 0 est 1 [prof1@228-poste10 shell]$ ./fact_while La factorielle de 1 est 1 [prof1@228-poste10 shell]$ ./fact_while La factorielle de 2 est 2 [prof1@228-poste10 shell]$ ./fact_while La factorielle de 3 est 6 [prof1@228-poste10 shell]$ ./fact_while La factorielle de 4 est 24 [prof1@228-poste10 shell]$ ./fact_while La factorielle de 5 est 120 [prof1@228-poste10 shell]$ ./fact_while La factorielle de 6 est 720 [prof1@228-poste10 shell]$ , 0 1 2 3 4 5 6 = > #! /bin/sh #boucle n fois en utilisant une structure while i=0 while [ $i -lt 12 ] do echo "i = $i" let $[ i += 1 ] done [prof1@228-poste10 shell]$ ./boucle_while i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 i = 10 i = 11 [prof1@228-poste10 shell]$ - &? $ 7; = 8 : 7 ?:? 8 : l 7 7?A 2 : 8 1 7 ?: 273 1 7; = 7 <7> : 8 : 8 F ?: N I ( =: > > <7B? 7 A 7C $ F ?: 7 C 7C D?7B?(I : B? : 8 7 A : 8 7A = > #! /bin/sh #calcul d'une factorielle avec une boucle until if [ $1 -eq 0 ] || [ $1 -eq 1 ] then echo "$1! = 1" else i=1 resultat=1 until [ $i -eq $1 ] do let $[ i += 1 ] let $[ resultat *= i ] done echo "$1! = $resultat" fi - : 8 B? [prof1@228-poste10 0! = 1 [prof1@228-poste10 1! = 1 [prof1@228-poste10 2! = 2 [prof1@228-poste10 3! = 6 [prof1@228-poste10 4! = 24 [prof1@228-poste10 5! = 120 [prof1@228-poste10 6! = 720 shell]$ ./fact_until 0 shell]$ ./fact_until 1 shell]$ ./fact_until 2 shell]$ ./fact_until 3 shell]$ ./fact_until 4 shell]$ ./fact_until 5 shell]$ ./fact_until 6 = > #! /bin/sh #boucle n fois utilisant une structure until i=0 until [ $i -eq 12 ] do echo "i = $i" let $[ i += 1 ] done [prof1@228-poste10 shell]$ ./boucle_until i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 i = 10 i = 11 - ) :7 $ 7; = 8 : 7 ?:? 8 : 36 A F 8< 3 7 ?: 273 k k 8< 3 7 ?: 273 k k 63 l 7 7?A B? 8< > ? J ? : F 7 8 >?7 ? 7: 7 8< # gc $ 8 ?F > A ? 7 87> 7 F > 8( 8 9 ? 7C> 8( 7 ?: 72B? 7 7C> C 7> ? > A ? 3 / 8 g 8< 9 8(? : 7 g = > #! /bin/sh #traitement de case avec combinaison de cas reponse=N while [ "$reponse" != "o" ] && [ "$reponse" != "O" ] do echo "Voulez vous travailler (o/n) ?" read reponse case "$reponse" in o | O ) echo "Bon courage";; n | N ) echo "Vous êtes peu courageux !";; * ) echo "Erreur : entrez o ou n !" esac done [prof1@228-poste10 shell]$ ./case.sh Voulez vous travailler (o/n) ? o Bon courage [prof1@228-poste10 shell]$ ./case.sh Voulez vous travailler (o/n) ? O Bon courage [prof1@228-poste10 shell]$ ./case.sh Voulez vous travailler (o/n) ? n Vous êtes peu courageux ! Voulez vous travailler (o/n) ? N Vous êtes peu courageux ! Voulez vous travailler (o/n) ? a Erreur : entrez o ou n ! Voulez vous travailler (o/n) ? o Bon courage [prof1@228-poste10 shell]$ ./case.sh 0 ) . 3 6 $ > 8 8C9 7 7> > 79 : 7 B?( 77 7I ? 7 7( > : ? 7: > = ' 7 ? >> > A B? > 7 : 8(? 7 ?7 @ 8 :? 9 : ::<7I ? 7 7A F 7@ > 77 ? : 7 8( A @ ? 89: 7 >7 : > 7B? 9 : 7 @ $ 7; = 8"? 9 : J? 3 7 7?A GH W : A : A VKA ? K 7 ?: 7 ?: ? A k X $ :C K : K> 8 8C9 8 7A F 7 : 7I 9 : $ 8 < 8 8 J C > ? > A ? ::<8 ?= > < 78(? 9 : I : ?=8(? 7: > R: ?=A F 7O[@ OZ @ OU@ O @ O @ B? 7 > 89C 7> 8 ? 8? C 8 9 : A : @ O0 : >7 $ ? 8(? A ? 7( 99:? R: ? :C ? ? ( 7 > 7 > ;C@ A ? 8 ? 7: 8 8 < : 8 =C:?C @ ( > 8 : 8 = 7? 9 : 7 ?77 7: > 9 : > ? J C:? 7A @ :( 7 I 8 B?( > ? 7( >> J 9 7 8C9 @ ? 9 : >> T 8 7 7 87 A F 7 > ? ( => A 7 7 ? 7 7 R: I : 8 =7 @? J? 3 : : = > #! /bin/sh #utilisation de fonctions carre() { return $[ $1 * $1 ] } if [ $# -ne 1 ] then echo "Usage ./fonction nombre" else carre $1 echo "le carré de $1 est $?" fi [prof1@228-poste10 shell]$ ./carre.sh 5 le carré de 5 est 25 [prof1@228-poste10 shell]$ ./carre.sh 16 le carré de 16 est 256 [prof1@228-poste10 shell]$ = > #! /bin/sh #utilisation de fonctions #calcul de a puissance b (a premier paramètre b second paramètre) puissance() { if [ $2 -eq 0 ]; then return 1 elif [ $2 -eq 1 ]; then return $1 else local i=1 local resultat=$1 while [ $i -lt $2 ] do let $[ resultat *= $1 ] let $[ i += 1 ] done return $resultat fi } if [ $# -ne 2 ] then echo "Usage ./fonction nombre puissance" else puissance $1 $2 echo "$1 puissance $2 vaut : $?" fi [prof1@228-poste10 5 puissance 2 vaut [prof1@228-poste10 2 puissance 5 vaut [prof1@228-poste10 3 puissance 3 vaut [prof1@228-poste10 4 puissance 4 vaut / 2 shell]$ : 25 shell]$ : 32 shell]$ : 27 shell]$ : 256 ./puissance.sh 5 2 ./puissance.sh 2 5 ./puissance.sh 3 3 ./puissance.sh 4 4 66 232 6 =432 5 6 0 9 78 : T >? J C 77 8 : \8" F " F 7: : 8"? 9: 6662 • • • • • 6?3 6G 1 362 OW: T UU X B? 7?>> : 7> 8 I A OW: T U X B? 7?>> : 7> 8 I A OW: T ff X B? 7?>> : 7> 8 I A OW: T f X B? 7?>> : 7> 8 I A OWU: T XB? A T 6 47 @ 8"? 9: I > 8 7 : > ? ? 7 7: 8 77?A 7 : > 6H > 8? 8CF?@ 7@ @ > 8? 8CF?@ 7@ @ > 8 9@ 7@ @ > 8 9@ 7@ ?? 8 : T @ >?7 >?7 > >?7 >?7 > 8 > 8 : T > 8 : T 8 > 8 : T > 8 : T = > #! /bin/sh # test d'occurence t="institut lemonnier" echo "chaine complète : $t" echo echo "plus petite occurence echo "plus grande occurence echo "plus petite occurence echo "plus grande occurence de de de de i i i i depuis depuis depuis depuis le le la la début début fin : fin : : ${t#*i}" : ${t##*i}" ${t%i*}" ${t%%i*}" [prof1@228-poste10 shell]$ ./occurence.sh chaine complète : institut lemonnier plus plus plus plus petite grande petite grande occurence occurence occurence occurence de de de de i i i i depuis depuis depuis depuis le le la la début : nstitut lemonnier début : er fin : institut lemonn fin : = > #! /bin/sh #extraction du nom de fichier #ou extraction du répertoire où se trouve le fichier nom="/usr/bin/passwd" echo "Longueur de la chaîne : ${#nom} caractères" echo "nom du fichier : ${nom##*/}" echo "chemin du fichier : ${nom%/*}" [prof1@228-poste10 shell]$ ./param1.sh Longueur de la chaîne : 15 caractères nom du fichier : passwd chemin du fichier : /usr/bin [prof1@228-poste10 shell]$ > 7 ?F 0 8 :? B? : :< [ : 7> 8I ? 8C C8 : :< 7 7 : > C7 ; > :? > 7: > : 77 I ? 7 ? 8 :? 7" =C:? : 7 7 78 7> A 8?7: > ? J 8 7 8 78 C 7I ? : 8 8 7? : > C 8 > I ? : 8 8 C 7I > 8?: A ?8"? 9: @ 7B?" 9 @ = > #! /bin/sh #utilisation de document incorporé if [ -f $1 ] then echo "Le fichier existe déjà !" else touch $1 cat >$1 <<!FIN1! #! /bin/sh # F ************************************ # # * script réalisé par J. NEVEU * # * * !FIN1! date '+# * le %A %d %B %Y à %H:%M *' >> $1 cat >> $1 <<!FIN2! # * * # ************************************ !FIN2! chmod u+x $1 fi [prof1@228-poste10 shell]$ ./doc_incorpore script1.sh [prof1@228-poste10 shell]$ ls -l script1.sh -rwxr--r-1 prof1 profs 274 jun 7 11:21 script1.sh* [prof1@228-poste10 shell]$ cat script1.sh #! /bin/sh # # # # # # ************************************ * script réalisé par J. NEVEU * * * * le jeudi 07 juin 2001 à 11:21 * * * ************************************ [prof1@228-poste10 shell]$ &
Documents pareils
LESSON 1, Classwork: http://www.bbc.co.uk/schools/primaryfrench
Ca va bien (mal) - I’m fine (I have
a problem)