LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General > Linux - Certification
User Name
Password
Linux - Certification This forum is for the discussion of all topics relating to Linux certification.

Notices


Reply
  Search this Thread
Old 04-11-2009, 01:31 PM   #1
siawash
Member
 
Registered: Jun 2004
Location: Ireland
Distribution: Zorin 6
Posts: 589

Rep: Reputation: 32
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.
 
Old 04-11-2009, 02:46 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Quote:
Originally Posted by siawash View Post
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 View Post
> 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).
 
Old 04-11-2009, 03:33 PM   #3
siawash
Member
 
Registered: Jun 2004
Location: Ireland
Distribution: Zorin 6
Posts: 589

Original Poster
Rep: Reputation: 32
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...

Last edited by siawash; 04-11-2009 at 03:36 PM.
 
Old 04-11-2009, 03:36 PM   #4
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
I believe you click on the thumbs up icon in the post you wish to thank.
 
Old 04-11-2009, 04:58 PM   #5
siawash
Member
 
Registered: Jun 2004
Location: Ireland
Distribution: Zorin 6
Posts: 589

Original Poster
Rep: Reputation: 32
As you can see I clicked on the thumb but my profile still shows zero thanked...
 
Old 04-11-2009, 05:06 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by siawash View Post
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
SHELL scripting/ shell functions mayaabboud Linux - Newbie 6 12-26-2007 08:18 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General > Linux - Certification

All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration