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