shell scripting exercise
Hi I am doing an exercise in an LPI manual which is not very clearly explained. Here is the script. I have inserted comments for each line with my assumptions. I would appreciate if someone could correct or explain these assumptions.
Here is the stand alone code: for var in arg1 arg2 arg3 do echo $var mkdir mydir5[$var] cd mydir5[$var] touch myfile5.txt cd done ************************************************************************** ASSUMPTION *********************************************************************** deb1:/home/siawash# for var in arg1 arg2 arg3 # within the users home directory create variables arg1, arg2 arg3 using #a "for" loop > do > echo $var # Then print out each variable name consecutively > mkdir mydir5[$var] # create a directory called mydir5. i get confused after this point. # What is meant to happen with the arg parameters.?? The effect is that I #end up with a directory called mydir5[arg1] in my user's home and myfile5.txt within it and mydir5[arg2], mydir[arg3] in my /root #directory. # I had the feeling that there should be three directories, each with different parameters..arg1 arg2 arg3 and each with myfile5.txt...am I wrong.? > cd mydir5[$var] # I had the feeling this bit is meant to go in mydir5[arg1] > touch myfile5.txt # and create myfile5.txt in each directory. > cd # Then cd out ##How does the script tell bash to "cd .." and go into /root ##and create mydir5[arg2] mydir5[arg3] > done ************************************************************************* Here is the output of that script arg1 arg2 arg3 ************************************************************************* This is the prompt with the ls output deb1:~# ls mydir5[arg2] mydir5[arg3] deb1:~# Here is how I understand it. |
Quote:
Quote:
Since you start in your user's $HOME, the first directory mydir5[arg1] is created there. The first cd at the end of the do loop puts you in /root. The command cd without a path defaults to the user's $HOME. If my guess about you executing the script as root is correct, that's why you end up with the next two directories in /root (root's $HOME). |
Yes, you were spot on. Many thanks for your reply!
BTW I notice that LQ has a feature that allows users to thank contributors, but I don't know how I can activate it. Would you be able to help? Kind of embarrassing receiving so much help from so many and not thanked any... |
I believe you click on the thumbs up icon in the post you wish to thank.
|
As you can see I clicked on the thumb but my profile still shows zero thanked...
|
Quote:
Now I thank your post, just to demonstrate that the count increase when you receive thanks from other members. Cheers! :) |
All times are GMT -5. The time now is 04:32 PM. |