I need translation!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I need translation!
# 1  
Old 04-05-2005
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
# 2  
Old 04-05-2005
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 ]
# 3  
Old 04-05-2005
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
# 4  
Old 04-05-2005
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.
# 5  
Old 04-05-2005
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
# 6  
Old 04-05-2005
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).
# 7  
Old 04-05-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. IP Networking

Network address translation

How would one approach the problem of determining the NAT tables of a router without knowing the userid and password. The only password holder died. I know the internal ip address of the router is 192.168.2.1, and also ports 80 or 8080 and 3389 are open. (5 Replies)
Discussion started by: jgt
5 Replies

2. AIX

Can't ping - IP translation issue?

Hello, Any help or advice on this would be greatly appreciated. I have a site-to-site VPN, across which data is synced between two CRM systems. The opposing side of this tunnel is a flat network (lets call it 201.200.156.xx/24) My internal network is a 192.168.0.xx/24 network, however before... (2 Replies)
Discussion started by: thruster9
2 Replies

3. UNIX Desktop Questions & Answers

Text Translation

hi all, Im a unix newbie and Im trying to find and replace text and I been looking at these posts since 7 am and I still dont see what Im after. Here is the task at hand. Im trying to translate few lines by doing cat <file> and here is what Im doing <file> 20110228 Mike Original apartment... (4 Replies)
Discussion started by: PG3
4 Replies

4. UNIX for Dummies Questions & Answers

AIX to HP-UX translation please

I have been looking up the allocated space, free space and the contents (raw, jfs, paging) of the logical volumes in AIX servers. I have been using lsvg and lsvg -l (server name). What is the HP-UX equivalent to that? I was given this command also so I would not have to type in each volume group... (2 Replies)
Discussion started by: compan023
2 Replies

5. UNIX for Dummies Questions & Answers

help --translation

I am reading the SANS security policy--.I want to translate the to Chinese.But I don`t understand a paragraph of the. It is :"Where SNMP is used, the community strings must be defined as something other than the standard defaults of "public," "private" and "system" and must be different from... (1 Reply)
Discussion started by: beijingzhzj
1 Replies

6. Programming

Swedish Character translation

Sorry if this been posted before. At the moment I'm having problem with the iscntrl() function. I need it to run a check on code entered into a Text Box, however I also need to allow the "Ö, ö" Swedish characters. The iscntrl() function seems to pick up the characters above as part of the... (0 Replies)
Discussion started by: rebelwassie
0 Replies

7. Shell Programming and Scripting

need translation?

Can someone tell me exactly what this code is doing? # # gnutest # # Test launch of ghostscript (gs) from a script # rm gnutest.ps # # ------- calculation that would generate file(s) to # be plotted with gnuplot would be placed here ----------- # gnuplot << \E-o-f2 set... (1 Reply)
Discussion started by: wmosley2
1 Replies
Login or Register to Ask a Question