![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
||||
|
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 ] |
|
||||
|
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 $@ users will carry values: unix, linux, aix, windows, solaris one at a time. vino |
|
|||
|
Quote:
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. Code:
for users in $@ do #your own code #and also egrep egrep "$x" path-of-file-to-be-grep'ed done 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. 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 |
||||
| Google The UNIX and Linux Forums |