TP3 Bash Command Operations and Scripting

1/1
100%

TP3

Exercice1: Séquence de commande

Dire pour chaque opérateur les commandes données:

1. s’exécutent parallèlement ou séquentiellement

2. Si l’exécution de la deuxième dépend de la première ou pas

  • Opérateur “;”

+ sleep 2; echo Bonjour

+ test 1 -eq 0; echo Bonjour

+ test 1 -eq 1; echo Bonjour

  • Opérateur “|”

+ sleep 2 | echo Bonjour

+ test 1 -eq 0 | echo Bonjour

+ test 1 -eq 1 | echo Bonjour

  • Opérateur “&&”

+ sleep 2 && echo Bonjour

+ test 1 -eq 0 && echo Bonjour

Publicité

+ test 1 -eq 1 && echo Bonjour

  • Opérateur “||”

+ sleep 2 || echo Bonjour

+ test 1 -eq 0 || echo Bonjour

+ test 1 -eq 1 || echo Bonjour

Exercice 2 Variables d’environnements

1. Dire pour chaque type de cotes (‘...’, “...”, ... ), si :

2. Les variables sont interprétées

3. Les commandes sont interprétées

Commandes:

(1) echo ’$USER execute pwd ’ (2) echo “$USER execute pwd

(3) echo ’$USER execute pwd’ (4) echo “$USER execute pwd“

1. Donnez les valeurs des variables suivants:

echo $USER

echo $UID

echo $HOME

Publicité

1. Que fait les commandes suivantes env

Exercice 3 Script

  • Créer le fichier script.bash

![](data:image/png;base64...)

  • le rendre exécutable: $ chmod +x script.bash
  • Tapper la commande $ ./script.bash

+ Que contienent les variables $0, $\* $@ et $#

  • Tapper la commande $ script.bash.

+ Que constatez-vous?

+ Editer la variables d’environnement PATH:

+ export PATH=$PATH:.

+ Re-tapper la commande $ script.bash. Que remarquer vous?

+ Déduire le rôle de la variables PATH

QCM

1. You type a command into bash and pass a long filename to it, but after you enter the command, you receive a File not found error message because of a typo in the filename. How might you proceed?

A. Retype the command, and be sure you type the filename correctly, letter by letter.

B. Retype the command, but press the Tab key after typing a few letters of the long filename to ensure that the filename is entered correctly.

Publicité

C. Press the Up arrow key, and use bash’s editing features to correct the typo.

D. Any of the above.

E. None of the above.

2. You type echo $PROC , and the computer replies Go away. What does this mean?

A. No currently running processes are associated with your shell, so you may log out without terminating them.

B. The remote computer PROC isn’t accepting connections; you should contact its administrator to correct the problem.

C. Your computer is handling too many processes; you must kill some of them to regain control of the computer.

D. Your central processing unit (CPU) is defective and must be replaced as soon as possible.

E. You, one of your configuration files, or a program you’ve run has set the $PROC environment variable to Go away.

3. What does the pwd command accomplish?

A. It prints the name of the working directory.

B. It changes the current working directory.

C. It prints wide displays on narrow paper.

D. It parses Web page URLs for display.

E. It prints the terminal’s width in characters.

4. What is the surest way to run a program (say, myprog) that’s located in the current working directory?

Publicité

A. Type ./ followed by the program name: ./myprog .

B. Type the program name alone: myprog .

C. Type run followed by the program name: run myprog .

D. Type /. followed by the program name: /.myprog .

E. Type the program name followed by an ampersand (&): myprog & .

5. How many commands can you pipe together at once?

A. 2

B. 3

C. 4

D. 16

E. An arbitrary number

TP3 Bash Command Operations and Scripting

Command Line Operations and Scripting · lab

Voir tous les documents en systèmes d'exploitation et cloud

TP3

Exercice1: Séquence de commande

Dire pour chaque opérateur les commandes données:

1. s’exécutent parallèlement ou séquentiellement

2. Si l’exécution de la deuxième dépend de la première ou pas

  • Opérateur “;”

+ sleep 2; echo Bonjour

+ test 1 -eq 0; echo Bonjour

+ test 1 -eq 1; echo Bonjour

  • Opérateur “|”

+ sleep 2 | echo Bonjour

+ test 1 -eq 0 | echo Bonjour

+ test 1 -eq 1 | echo Bonjour

  • Opérateur “&&”

+ sleep 2 && echo Bonjour

+ test 1 -eq 0 && echo Bonjour

Publicité

+ test 1 -eq 1 && echo Bonjour

  • Opérateur “||”

+ sleep 2 || echo Bonjour

+ test 1 -eq 0 || echo Bonjour

+ test 1 -eq 1 || echo Bonjour

Exercice 2 Variables d’environnements

1. Dire pour chaque type de cotes (‘...’, “...”, ... ), si :

2. Les variables sont interprétées

3. Les commandes sont interprétées

Commandes:

(1) echo ’$USER execute pwd ’ (2) echo “$USER execute pwd

(3) echo ’$USER execute pwd’ (4) echo “$USER execute pwd“

1. Donnez les valeurs des variables suivants:

echo $USER

echo $UID

echo $HOME

Publicité

1. Que fait les commandes suivantes env

Exercice 3 Script

  • Créer le fichier script.bash

![](data:image/png;base64...)

  • le rendre exécutable: $ chmod +x script.bash
  • Tapper la commande $ ./script.bash

+ Que contienent les variables $0, $\* $@ et $#

  • Tapper la commande $ script.bash.

+ Que constatez-vous?

+ Editer la variables d’environnement PATH:

+ export PATH=$PATH:.

+ Re-tapper la commande $ script.bash. Que remarquer vous?

+ Déduire le rôle de la variables PATH

QCM

1. You type a command into bash and pass a long filename to it, but after you enter the command, you receive a File not found error message because of a typo in the filename. How might you proceed?

A. Retype the command, and be sure you type the filename correctly, letter by letter.

B. Retype the command, but press the Tab key after typing a few letters of the long filename to ensure that the filename is entered correctly.

Publicité

C. Press the Up arrow key, and use bash’s editing features to correct the typo.

D. Any of the above.

E. None of the above.

2. You type echo $PROC , and the computer replies Go away. What does this mean?

A. No currently running processes are associated with your shell, so you may log out without terminating them.

B. The remote computer PROC isn’t accepting connections; you should contact its administrator to correct the problem.

C. Your computer is handling too many processes; you must kill some of them to regain control of the computer.

D. Your central processing unit (CPU) is defective and must be replaced as soon as possible.

E. You, one of your configuration files, or a program you’ve run has set the $PROC environment variable to Go away.

3. What does the pwd command accomplish?

A. It prints the name of the working directory.

B. It changes the current working directory.

C. It prints wide displays on narrow paper.

D. It parses Web page URLs for display.

E. It prints the terminal’s width in characters.

4. What is the surest way to run a program (say, myprog) that’s located in the current working directory?

Publicité

A. Type ./ followed by the program name: ./myprog .

B. Type the program name alone: myprog .

C. Type run followed by the program name: run myprog .

D. Type /. followed by the program name: /.myprog .

E. Type the program name followed by an ampersand (&): myprog & .

5. How many commands can you pipe together at once?

A. 2

B. 3

C. 4

D. 16

E. An arbitrary number