Bash Scripting Intro
- Create a folder on your Linux server named a40bashcount. Make sure that your linux server has at least two user accounts and that the user accounts have at least a couple of files and folders (the names and contents of these files/folders are not important).
- Write a bash shell script that takes the name of a user as a parameters and counts the number of files in that user's home folder (and sub-folders).
-
The output of the command should look similar to this:
$ ./countfiles r.ford Report for files in r.ford home folder Home folder: /home/r.ford The user has 223 files/foldersPlease note that if you run this on an account that is not yours, you will likely see error messages. With some searching online, you should be able to find a way to supress the error message. -
Some hints about what goes into the script. You can try the steps 2 and 3
on a command line by themselves before trying to use them in the script.
- Check to see if the parameter has been set and stop and print an error if it is missing.
- Get the home folder of the user from the /etc/passwd file. The grep and cut command will be helpful here.
- Once you know the user's home folder, you can use the find command to find everything and count them using wc.
- If the user account does not exist then you should print a message like "User r.ford does not exist on this server".
- When you are finished, show the contents of the script and show the script being run with a good user and a non-existing user.