LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2006, 07:44 AM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47

#!/bin/sh
set -e
echo "The amount of commandline args is $#"
echo "They are \"$@\""
n=0; for arg in $@;

do

if

[ "$arg" = "$LOGNAME" ];

then

echo "Number $n is logname"

fi

let n=${n}+1; shift $n;

done

exit 0
-----------------------------------

I saved the above program as 'argument2'.

When I write 'env' at the commoand line, it says 'LOGNAME=nissanka' and a lot of other details.

I ran the program by giving 3 arguments. They are 'nissanka' , 'df' and 'du'.


[nissanka@c83-250-110-112 ~]$ ./argument2 nissanka df du
The amount of commandline args is 3
They are "nissanka df du"
Number 0 is logname
[nissanka@c83-250-110-112 ~]$

I have a few questions on this.
1.Why did I get number 0 logname?

2. What is this line doing ---> let n=${n}+1; shift $n;

I would like to hear from you all.
 
Old 08-27-2006, 09:43 AM   #2
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
I urge someone to provide me a with a reply. I am a learner too. When I find time, I look at scripting language by running a small program or two.
 
Old 08-28-2006, 10:51 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Why did I get number 0 logname?
Because of:
Code:
# Initialise "counter"
n=0
# Take all commandline arguments and loop over them
for arg in nissanka df du;
 # If the argument we're inspecting matches the logname
 # of the currently logged in user, echo some text
 if [ nissanka = nissanka ]; then
  echo "Number 0 is logname"
 fi
It's easier to see values get filled in if you run the script as: "sh -x argument2 nissanka df du".


What is this line doing ---> let n=${n}+
"let" ("help let") does arithmetic expressions, in this case it takes the value of the counter and increments it by one. Another notation could be ((n++)) or n=$[${n}+1]


; shift $n;
When executing a Bash shell script the scriptname occupies variable "$0". Commandline arguments take up positions 1 through * where 1 through 9 are available immediately. If you would like to see the next one you use "shift" ("help shift") "to, ahhh, shift focus.


I urge someone to provide me a with a reply.
...and that's the reason we had your posts moved to your own brand new thread. If we didn't people would think you just replied to the OP (which you didnt, which in the worse cases is called thread hijacking) and would easily miss your questions. So: your questions -> your own thread. And you best wait 24 hours before asking for a reply. Zero-reply threads get bumped automagically so you only have to work on your patience.
 
Old 08-28-2006, 12:26 PM   #4
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
unSpawn, it was an excellent lengthy reply. Thank you very much.

As a moderator you could replace or remove my threads. I respect to your inalienable rights.
 
  


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
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 AM.

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