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

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 ]

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,

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

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.

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.

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

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.

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.

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

I got it working. Thanks for the help bro! :slight_smile: