Copy/Paste your script from 3d here.
The DOS scripting language is not very useful and instead most people now use a language called Power Shell. Instead of exploring this we will explore Linux scripting using the Bash scripting language. Shut down your Windows VM and start your Linux VM, opening two terminal windows. In one, type cd ~ followed by mkdir scripts followed by cd scripts. In the other, cd ~/scripts. In one window, you will write scripts using vi and in the other you will run them. a. Type vi script1.sh In vi, type i to enter insert mode, enter this following script and then type
to exit insert mode followed by: w to save the script. #!/bin/bash echo Hello $USER In this script, $USER is an environment variable defined in one of the Bash start-up scripts. To run this script, you type ./script1.sh. Do so from the window with the command line prompt (the one not running vi). What happens? In order to run any script, it must be executable, but by default, text files are not executable. We will use chmod to change the file's permission to 755 (you can also set it to 745). Do so. What command did you enter? Run the script. What is the output? b. As with DOS, we can pass parameters to a script and reference them using $1, $2, $3, etc. Revise this script to output Hello $1. In vi, move the cursor to the beginning of $USER, press the key until it is deleted, type i to enter insert mode, type $1 and then: wq to save and exit the file. Run it as ./script1.sh. What is the output? How do you run it so that the output is Hello your name? c. If you run env, you will see other environment variables. From your command line, enter the command cp script1.sh script2.sh to create a second script (note in copying scnpt1.sh, you are also copying its permissions so that you will not have to change $cript2.sh's permissions). In your other window, type vi script2.sh so that you can modify a new version of the script. In this case, we want to output the user's username, home directory, current working directory, default language and machine's host name, all on separate lines, each using its own echo statement. First, you will have to return $1 to $USER. Next, add the extra echo statements. To enter insert mode, type i. When done, type to exit insert mode and: w to save the file. Run the program in the other window. When it looks satisfactory, return to the file and add one instruction (type G to reach the end of the file and o to insert a blank line at the bottom), echo Todays date is date. Type to exit input mode and save the file (:w) and run it. What Is the output? d. We can fix this problem by listing the word date by itself on a separate line (outside of an echo statement) but then we don't see "Todays date is". There are two ways to fix this then. First, move the last instance of date onto a separate line, in which case we see as output Todays date is on one line and the date on a separate line, so we resolve this by using echo -n instead of echo. The other solution is to place the last instance of date in either " or $() marks. Solve this by both approaches. Run the script after doing the first approach. Change the script to the second approach and rerun the script. Why is the second approach better? When done, make sure you have implemented this using the latter approach (using " or $()) and insert your final version of this script in your answer file. Exit vi by typing: q (or:wq if you have made any changes and not yet saved them). Shut down your Linux VM (or continue with lab 14)