Lab 4a Shell Script Lab 2 Using echo command in shell script Objective Upon completion of this lab, the student will be able to use echo command in the shell script. Scenario The student is the administrator of a Linux server who wants to use echo command in the shell script. Procedures Login into Fedora operating system. Use a text editor such as gedit, vi or emacs. Write required Linux commands and logic in the file. Save and close the file (exit from gedit, vi or emacs). Make the script executable. Test the script by running it, and verify the output. Step 1: Creating the Script 1. Login into Fedora operating system. 2. Launch gedit Text Editor by clicking on Applications Accessories gedit Text Editor 3. Type in the following using gedit text editor: echo This is my first echo statement. echo This is my second echo statement. 4. Save the text as myecho.sh. 5. Go to terminal by click on Application System Tools Terminal 6. Make the file executable and run the script by typing the following commands in the terminal. chmod +x myecho.sh./myecho.sh This is my first echo statement. This is my second echo statement. 1
Step 2: Modify the assignment statement. 1. Open the file myname.sh using gedit text editor. 2. Modify the code as follow by removing the double quotes in line 2 and 3: echo This is my first echo statement. echo This is my second echo statement. 3. Save the text and run the script by typing the following command in the terminal../myecho.sh This is my first echo statement. This is my second echo statement. Reflection: 1) Write a script that will display as follow: 2
3
Lab 4b Shell Script Lab 3 Using assignment statement in shell script Objective Upon completion of this lab, the student will be able to use assignment statement in Linux shell script. Scenario The student is the administrator of a Linux server who wants to create a simple shell script using assignment statement. Procedures Login into Fedora operating system. Use a text editor such as gedit, vi or emacs. Write required Linux commands and logic in the file. Save and close the file (exit from gedit, vi or emacs). Make the script executable. Test the script by running it, and verify the output. Step 1: Creating the Script 1. Login into Fedora operating system. 2. Launch gedit Text Editor by clicking on Applications Accessories gedit Text Editor 3. Type in the following using gedit text editor: MyName= Hello World echo $MyName 4. Save the text as myname.sh. 5. Go to terminal by click on Application System Tools Terminal 6. Make the file executable and run the script by typing the following commands in the terminal. chmod +x myname.sh./myname.sh Hello World Explanation: 4
The second line of code assigns the string "Hello World" to the variable MyName. The three line of code echoes out the value of the variable. Step 2: Modify the assignment statement. 1. Open the file myname.sh using gedit text editor. 2. Modify the code as follow by inserting a few space between MyName and = : MyName = Hello World echo $MyName 3. Save the text and run the script by typing the following commands in the terminal../myname.sh./pass.sh: line 2: MyName: command not found You will notice that the script is not able to run. It is because the shell treats MyName as the name of a command and tries to execute it. In order to make the shell treats MyName as a variable, make sure no spaces are around the "=" sign: MyName= Hello World (works) MyName = Hello World (doesn't work) Step 3: Modifying the echo statement 1. Open the file myname.sh using gedit text editor. 2. Modify the code as follow by removing the space entered in step 3 and the two double quotes in line 3: MyName= Hello World echo $MyName 3. Save the text and run the script by typing the following commands in the terminal. chmod +x myname.sh./myname.sh Hello World 5
As we can see the script is able to run properly with or without a double quotes around the variable. Reflection: 1) What will happen if double quotes are removed in line 2 of the script as followed: MyName=Hello World echo $MyName 2) What will happen if $ sign is removed in line 3 of the script as followed: MyName=Hello World echo MyName 6