The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
S-286: PHP Path Translation Vulnerability iBot Security Advisories (RSS) 0 05-09-2008 09:00 AM
help --translation beijingzhzj UNIX for Dummies Questions & Answers 1 11-10-2006 03:26 PM
perl translation - date command junkmail426 Shell Programming and Scripting 1 09-23-2005 04:30 PM
Swedish Character translation rebelwassie High Level Programming 0 10-27-2004 08:04 AM
need translation? wmosley2 Shell Programming and Scripting 1 12-15-2003 01:03 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-05-2005
Registered User
 

Join Date: Apr 2005
Posts: 10
Stumble this Post!
I need translation!

Someone give me info on this script. I put all the parts I do not understand in BOLD. Please someone explain what the parts in BOLD does/mean. Thanks!

#!/bin/bash
#Usage: cmdArgs.sh arg1 arg2 arg3
printf "The number of command line args is: $#\n"
declare -i counter #Not strictly necessary to declare
echo "The name of this script is: $0"
let counter=1
for x in $@
do
#Test if the argument is also a file
if [[ -f $x ]]
then
printf "The Argument is also a file... "
fi
echo "Argument #$counter is: $x"
let counter=$counter+1
done
if [[ $# < 1 ]]
then
echo "You need to add some command line args"
exit 1
else
echo "Done!"
fi
exit 0
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-05-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Stumble this Post!
let counter=1
You are initializing the variable counter to 1.

for x in $@

Suppose your input at the prompt is $> sh myscript.sh arg1 arg2 arg3

$@ will take arg1, arg2 and arg3 as a single string i.e. it will hold it as "arg1 arg2 arg3"

for x in $@ means,
for each of the arg in $@ i.e. arg1, arg2, arg3 taken one at a time.

if [[ -f $x ]]
means, test if $x is a file or not. -f refers to the True if file exists and is a regular file

$x refers to the individual arg in context, i.e. (in this case) arg1 , arg2..et al.

let counter=$counter+1
You are incrementing the numerical value of counter.

Help yourself to some scripting tutorial at http://quong.best.vwh.net/shellin20/

That should answer your question.

While on this, a question for others, why do we need if [[ -f $x ]] instead of if [ -f $x ]
Reply With Quote
  #3 (permalink)  
Old 04-05-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Stumble this Post!
Just another thought.

If this script happens to go on and become part of a bigger script, the test for number of arguments should happen right at the beginning. In you case,
Code:
if [[ $# < 1 ]]
then
echo "You need to add some command line args"
exit 1
else
echo "Done!"
fi
should appear at the beginning. Reason being, script are traversed sequentially.

Vino
Reply With Quote
  #4 (permalink)  
Old 04-05-2005
Registered User
 

Join Date: Apr 2005
Posts: 10
Stumble this Post!
what is the counter used for in the script??

Another thing... if I was to initiate this for users in $@ in my script. How would I take each argument stored (as a string) in users so I can use each argument in a loop one by one.
Reply With Quote
  #5 (permalink)  
Old 04-05-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Stumble this Post!
In your script, counter is just used to count the number of arguments.

Say, your input was cmdArgs.sh unix linux aix windows solaris

counter will eventually end up having a value of 5.

You dont have to worry about each arg separately.

Code:
for users in $@
would do it for you.

users will carry values: unix, linux, aix, windows, solaris one at a time.

vino
Reply With Quote
  #6 (permalink)  
Old 04-05-2005
Registered User
 

Join Date: Apr 2005
Posts: 10
Stumble this Post!
Quote:
Originally Posted by vino
In your script, counter is just used to count the number of arguments.

Say, your input was cmdArgs.sh unix linux aix windows solaris

counter will eventually end up having a value of 5.

You dont have to worry about each arg separately.

Code:
for users in $@
would do it for you.

users will carry values: unix, linux, aix, windows, solaris one at a time.

vino

so say I wanted to take the first argument from users and use that in egrep.

How would I write that?

I only know how to write it in this manner "egrep "^$1" file.txt" (for argument 1) or "egrep "^$2" file.txt " (for argument 2).
Reply With Quote
  #7 (permalink)  
Old 04-05-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Stumble this Post!
As far as my understanding goes, this is how it can be done. And yes, please do correct me if its wrong. Am still learning !!

You can do it inside the for loop if you need. Or you can do it outside the loop too.

Inside loop.

Code:
for users in $@
do
#your own code
#and also egrep
egrep "$x" path-of-file-to-be-grep'ed
done
outside loop.
Here you might want to save each name in its own variable. And then later invoke egrep with those variables.
Code:
for users in $@
do
#your own code
#and now saving names.
#you could make use of your counter variable here.
#var1 will hold unix, and so on and so forth.
var$counter=users
let counter=$counter+1
done
for i in $counter
do
egrep "var$i" path-of-file-to-be-grep'ed
done.
Problems I see with each:
Inner.
-Doing grep while reading through names is not good. Possibly might break the loop in between if some grep-error arises. You might want to process all the names and then do a grep.

Outer.
Two for-loops. Surely not very efficient I think.

I'd still go for the outer

Moderators and other users, please suggest/comment/advise on this solution.

Thanks,
Vino
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0