1. When you type `passwd`, which file is executed?
2. What kind of file is that?
3. Execute the `pwd` command twice. (remember 0.)
4. Execute `ls` after `cd /etc`, but only if `cd /etc` did not error.
5. Execute `cd /etc` after `cd etc,` but only if `cd etc` fails.
6. Echo `it worked` when `touch test42` works, and echo `it failed` when the touch failed. All on one command line as a normal user (not root). Test this line in your home directory and in `/bin/`.
7. Execute `sleep 6`, what is this command doing ?
8. Execute `sleep 200` in background (do not wait for it to finish).
9. Write a command line that executes `rm file55`. Your command line should print `success` if file55 is removed, and print `failed` if there was a problem.
10. (optional) Use echo to display `"Hello World with strange' characters \ * [ } ~ \ \ ."` (including all quotes)
1. Find the list of shell options in the man page of bash. What is the difference between `set -u` and `set -o nounset`?
2. Activate `nounset` in your shell. Test that it shows an error message when using non-existing variables.
3. Deactivate `nounset`.
4. Execute `cd /var` and `ls` in an embedded shell. The echo command is only needed to show the result of the ls command. Omitting will result in the shell trying to execute the first file as a command.
5. Create the variable `embvar` in an embedded shell and echo it. Does the variable exist in your current shell now ?
6. Explain what `set -x` does. Can this be useful ?
7. (optional) Given the following screenshot, add exactly four characters to that command line so that the total output is `FirstMiddleLast`. `[paul@RHEL4b ~]$ echo First; echo Middle; echo Last`
8. Display a long listing (`ls -l`) of the `passwd` command using the `which` command inside an embedded shell.
8. Make sure your current bash shell remembers the next 5000 commands you type.
9. Open more than one console (by press Ctrl-shift-t in gnome-terminal, or by opening an extra putty.exe in MS Windows) with the same user account. When is command history written to the history file ?
## 17.7. practice: shell globbing
1. Create a test directory and enter it.
2. Create the following files : (the last one has 6 characters including a space)
15. Show the influence of `$LANG` in listing A-Z or a-z ranges.
16. You receive information that one of your servers was cracked, the cracker probably replaced the `ls` command. You know that the `echo` command is safe to use. Can echo replace ls ? How can you list the files in the current directory with echo?
17. Is there another command besides `cd` to change directories?
2. Verify that `noclobber` is active by repeating an `ls` on `/etc/` with redirected output to a file.
3. When listing all shell options, which character represents the `noclobber` option ?
4. Deactivate the `noclobber` option.
5. Make sure you have two shells open on the same computer. Create an empty tailing.txt file. Then type `tail -f tailing.txt`. Use the second shell to append a line of text to that file. Verify that the first shell displays this line.
6. Create a file that contains the names of five people. Use `cat` and output redirection to create the file and use a here document to end the input.
1. Explain the difference between these two commands. This question is very important. If you don't know the answer, then look back at the shell chapter.
*`find /data -name "*.txt"`
*`find /data -name *.txt`
2. Explain the difference between these two statements. Will they both work when there are 200 .odf files in /data ? How about when there are 2 million .odf files ?
*`find /data -name "*.odf" > data_odf.txt`
*`find /data/*.odf > data_odf.txt`
3. Write a find command that finds all files created after January 30th 2010.
4. Write a find command that finds all `*.odf` files created in September 2009.
5. Count the number of `*.conf` files in /etc and all its subdirs.
6. Here are two commands that do the same thing: copy `*.odf` files to /backup/ . What would be a reason to replace the first command with the second ? Again, this is an important question.
3. What 3 key sequence in command mode will switch two lines' place (line five becomes line six and line six becomes line five).
4. What 2 key sequence in command mode will switch a character's place with the next one.
5. vi can understand macro's. A macro can be recorded with q followed by the name of the macro. So qa will record the macro named a. Pressing q again will end the recording. You can recall the macro with @ followed by the name of the macro. Try this example: i 1 'Escape Key' qa yyp 'Ctrl a' q 5@a (Ctrl a will increase the number with one).
6. Copy `/etc/passwd` to your `~/passwd`. Open the last one in vi and press Ctrl v. Use the arrow keys to select a Visual Block, you can copy this with y or delete it with d. Try pasting it.
7. What does dwwP do when you are at the beginning of a word in a sentence ?
## 23.9. practice: introduction to scripting
0. Give each script a different name, keep them for later!
1. Write a script that outputs the name of a city.
2. Make sure the script runs in the bash shell.
3. Make sure the script runs in the Korn shell.
4. Create a script that defines two variables, and outputs their value.
5. The previous script does not influence your current shell (the variables do not exist outside of the script). Now run the script so that it influences your current shell.
6. Is there a shorter way to source the script ?
7. Comment your scripts so that you know what they are doing.
## 24.7. practice: scripting tests and loops
1. Write a script that uses a for loop to count from 3 to 7.
2. Write a script that uses a for loop to count from 1 to 17000.
3. Write a script that uses a while loop to count from 3 to 7.
4. Write a script that uses an until loop to count down from 8 to 4.
5. Write a script that counts the number of files ending in .txt in the current directory.
6. Wrap an if statement around the script so it is also correct when there are zero files ending in .txt.
## 25.7. practice: parameters and options
1. Write a script that receives four parameters, and outputs them in reverse order.
2. Write a script that receives two parameters (two filenames) and outputs whether those files exist.
3. Write a script that asks for a filename. Verify existence of the file, then verify that you own the file, and whether it is writable. If not, then make it writable.
4. Make a configuration file for the previous script. Put a logging switch in the config file, logging means writing detailed output of everything the script does to a log file in `/tmp`.
2. Improve the previous script to test that the numbers are between 1 and 100, exit with an error if necessary.
3. Improve the previous script to congratulate the user if the sum equals the product.
4. Write a script with a case insensitive case statement, using the shopt nocasematch option. The nocasematch option is reset to the value it had before the scripts started.
5. If time permits (or if you are waiting for other students to finish this practice), take a look at Linux system scripts in `/etc/init.d` and `/etc/rc.d` and try to understand them. Where does execution of a script start in `/etc/init.d/samba`? There are also some hidden scripts in `~`, we will discuss them later.
5. Use `su` to switch to another user account (unless you are root, you will need the password of the other account). And get back to the previous account.
6. Now use `su -` to switch to another user and notice the difference. Note that `su -` gets you into the home directory of Tania.
7. Try to create a new user account (when using your normal user account). this should fail. (Details on adding user accounts are explained in the next chapter.)
1. Create a user account named serena, including a home directory and a description (or comment) that reads Serena Williams. Do all this in one single command.
2. Create a user named venus, including home directory, bash shell, a description that reads Venus Williams all in one single command.
7. What happens when you log on with the einstime user ? Can you think of a useful real world example for changing a user's login shell to an application ?
2. Also set a password for venus and then lock the venus user account with `usermod`. Verify the locking in `/etc/shadow` before and after you lock it.
3. Use `passwd -d` to disable the serena password. Verify the serena line in `/etc/shadow` before and after disabling.
4. What is the difference between locking a user account and disabling a user account's password like we just did with `usermod -L` and `passwd -d`?
5. Try changing the password of serena to serena as serena.
6. Make sure serena has to change her password in 10 days.
8. Take a backup as root of `/etc/shadow`. Use `vi` to copy an encrypted hunter2 hash from venus to serena. Can serena now log on with hunter2 as a password ?
9. Why use `vipw` instead of `vi`? What could be the problem when using `vi` or `vim`?
10. Use `chsh` to list all shells (only works on RHEL/CentOS/Fedora), and compare to `cat /etc/shells`.
11. Which `useradd` option allows you to name a home directory?
12. How can you see whether the password of user serena is locked or unlocked ? Give a solution with `grep` and a solution with `passwd`.
4. Try several different ways to obtain a shell (su, su -, ssh, tmux, gnome-terminal, Ctrl- alt-F1, ...) and verify which of your custom variables, aliases and function are present in your environment.
5. Do you also know the order in which they are executed?
6. When an application depends on a setting in `$HOME/.profile`, does it matter whether `$HOME/.bash_profile` exists or not?
14. Create a directory that belongs to a group, where every member of that group can read and write to files, and create files. Make sure that people can only delete their own files.
1. Members of the sports group should be able to create files in this directory.
1. All files created in this directory should be group-owned by the sports group.
1. Users should be able to delete only their own user-owned files.
1. Test that this works!
2. Verify the permissions on `/usr/bin/passwd`. Remove the `setuid`, then try changing your password as a normal user. Reset the permissions back and try again.
3. If time permits (or if you are waiting for other students to finish this practice), read about file attributes in the man page of `chattr` and `lsattr`. Try setting the i attribute on a file and test that it works.