LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Certification (https://www.linuxquestions.org/questions/linux-certification-46/)
-   -   shell scripting exercise (https://www.linuxquestions.org/questions/linux-certification-46/shell-scripting-exercise-718463/)

siawash 04-11-2009 01:31 PM

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.

weibullguy 04-11-2009 02:46 PM

Quote:

Originally Posted by siawash (Post 3505678)
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

Correct. and correct.

Quote:

Originally Posted by siawash (Post 3505678)
> 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 would guess you are executing the script as root. Generally not a good idea when you don't understand what the script does. I'm also guessing that the script resides in your user's $HOME.

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).

siawash 04-11-2009 03:33 PM

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...

weibullguy 04-11-2009 03:36 PM

I believe you click on the thumbs up icon in the post you wish to thank.

siawash 04-11-2009 04:58 PM

As you can see I clicked on the thumb but my profile still shows zero thanked...

colucix 04-11-2009 05:06 PM

Quote:

Originally Posted by siawash (Post 3505821)
As you can see I clicked on the thumb but my profile still shows zero thanked...

If you check the statistics in your profile, you will notice that you have Total thanks: 1 and Thanked: 0. In your posts you see only the number of received thanks, not the number of given thanks.

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.