Assignment: bash (part 1)



Assignment2: (part 1)

Read everything before doing anything!

Please go through this exercise once to try everything out; when you turn in your script file, I don’t want to see every bad attempt you made to get something to work. I just want to see the results of working commands. Hint: Write down what you think the correct answers are, then try them. Once you get everything working, go through all the steps and record them in a script file.

Before You Start

1. In your home directory, create a file called info.sh which contains this text. On the last line of the script, that is a digit one before the two closing parentheses.

1. echo "Sample script."

2. echo "The date and time is $(date)"

echo "Number of processes: $(( $(ps -ef | wc --lines) - 1 ))"

2. In your home directory, create a file called myname.sh which contains this text. On the last line of the script, that is a digit one before the two closing parentheses.

1. echo "Sample script."

2. echo "The date and time is $(date)"

echo "My user name $(whoami)"

3. Use chmod to make myname.sh executable. Do not make info.sh executable.

4. At the end of your ~/.bashrc file, add this line:

echo "Starting shell on $(date)"

The Tasks

1. Type script bash1.script to start recording your commands.

2. Do a single ls command to determine which of the following exist: ~/.bash_profile, ~/.bash_login, and ~/.profile.

3. Do an su to your own username; that is, if you are user linux199, type su linux199

When asked for your password, enter it. You should get the message you entered in the previous step.

4. Type exit to go back to your original shell.

5. Try typing ./info.sh; this should generate an error message, because you have not made the script executable.

6. Run the commands in info.sh by typing source info.sh

7. Run the commands in myname.sh by typing ./myname.sh; that should work fine.

8. Do a single ls -l command on the files /etc/passwd, /etc/nosuchfile, and /etc/services. Redirect the standard output to a file named filelist.txt and standard error to a file named errors.txt

9. cat filelist.txt and then cat errors.txt putting both commands on one line (hint: see page 267).

10. Type the following commands:

1. cd

2. pushd /etc

3. pushd /usr/lib

4. popd

5. pushd /bin

6. pushd

11. Now use the popd command to return to your home directory (~). You may have to use it more than once!

12. Create a variable named me whose value is your full name. If your name is Joe Doakes, then the variable must have the string Joe Doakes in it, including the blank.

13. Using the variable you created in the previous step, write an echo command that will produce the output:

My name is full name

14. With one command, declare a variable named quantity to be of type integer and set it to the value 1789. Verify that you have done this correctly by typing echo $quantity

15. Assign the string "hello" to variable quantity, and then repeat echo $quantity to see what happens when you assign a non-number to a variable that is declared to hold integers.

16. Save your primary and secondary prompt variables by typing the following:

save1=$PS1; save2=$PS2

17. Set your primary prompt to the current time of day in 12-hour AM/PM format followed by a space, followed by the pathname of the working directory, followed by a space, a greater than sign, and a space (see page 287).

18. Set your secondary prompt to two greater than signs, followed by a space, then verify it by typing (on two separate lines):

19. echo "command that needs

secondary prompt"

20. Restore your primary and secondary prompt variables by typing the following:

PS1=$save1; PS2=$save2

21. Type cd to return to your home directory.

22. Type cd init.d; this should fail, since there is no directory named init.d in your home directory.

23. Make a directory called testdir (it may already exist; if so, that’s fine).

24. Set your CDPATH variable to /etc

25. Type cd testdir

26. Type cd init.d; it should succeed, since CDPATH has been changed.

27. Type exit to end the script.

When You Finish

Change the name of your script file to the form firstname_bash1.script and send to my e-mail saif@uonizwa.edu.om

Assignment2: (part 2)

Read everything before doing anything!

Please go through this exercise once to try everything out; when you turn in your script file, I don’t want to see every bad attempt you made to get something to work. I just want to see the results of working commands. Hint: Write down what you think the correct answers are, then try them. Once you get everything working, go through all the steps and record them in a script file.

The Tasks

Processes

1. Type script bash2 to start recording commands.

2. Type these commands:

1. sleep 15 &

2. ps --forest

3. Type these commands, one per line:

1. date

2. pwd

3. cd

4. df –human

5. who

4. Write a single echo command that shows the value of the variables HISTSIZE, HISTFILE, and HISTFILESIZE. (Hint: use $)

5. Show the last five commands you’ve typed by entering history 5

6. Use the fc command to show every command you have typed since the most recent ps command, and only those commands.

7. Type ls file and then the TAB key three times to see all filenames beginning with file.

8. Type echo $OLDP followed by the TAB key to see variable completion in action.

9. Create an alias dfh which is an alias for df --human

10. Type dfh to see if it worked.

11. Show a list of all aliases currently available.

12. Type cd to return to your home directory.

13. The next few examples are very important. Type this command:

alias dirA="echo Working Directory is $PWD"

Because the text is enclosed in double quotes, the variable $PWD is evaluated right then and there, as the alias is created, and the alias will contain the name of your home directory.

14. Now type this command:

alias dirB='echo Working Directory is $PWD'

Because the text is enclosed in single quotes, the alias will contain the literal string $PWD; it will not be evaluated right away.

15. Change to a different directory: cd /etc

16. Type dirA; since the home directory name was expanded when the alias was built, it shows you your home directory name, which is wrong.

17. Type dirB; since the $PWD is in single quotes, it gets evaluated when the alias gets used.

18. Use a single unalias command to get rid of the aliases dirA and dirB.

19. Use the alias command with no arguments to verify that those aliases are gone.

20. This one is just too cool to pass up: type cd /user; it should give you an error message (it’s the /usr directory, not /user).

21. Now type this: shopt -s cdspell

22. Now type cd /user again. It’s magic!

23. Now turn off the cdspell option. (use -u)

24. Confirm it by typing cd /user again, and getting an error message.

25. Type exit to stop recording your commands in the script file.

When You Finish

Change the name of your script file to the form firstname_bash2.script and send to my e-mail saif@uonizwa.edu.om

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download