How to pass different number of arguments in a single shot


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass different number of arguments in a single shot
# 1  
Old 02-13-2012
How to pass different number of arguments in a single shot

I have one configuration file. The number of lines in the file will vary. I need to pass each line as a parameter to a shell script in a single shot.
Ex:
Suppose file contains:
ou=x,o=z
o=y

Suppose the shell script name is sample.sh. Then the script should be called like sample.sh ou=x.o=z o=y
Like said previously no of lines in the file will vary. I know how to read line by line pass each line to shell script. But here I need to pass all the arguments in one shot. Will argList = $argList + "$line" will suffice?
# 2  
Old 02-13-2012
MySQL

try the below scripts. it works as you expected unless there is a space.

te4:
 
#! /bin/bash
val=`awk '{
i=0
printf("%s ",$i)
}' inp4`
./te5 $val
te5: prints the argument received
 
#! /bin/bash
echo "val 1 $1"
echo "val 2 $2"
inp4: input file
 
ou=x,o=z
o=y
Output is:
./te4
val 1 ou=x,o=z
val 2 o=y

Thanks
kalai
This User Gave Thanks to kalpeer For This Post:
# 3  
Old 02-13-2012
Hi kalpeer,
Thanks for your reply. Actually the file may contain some line starting with #. Those lines we need to ignore. Also we need to ignore if there are any blank lines. So I think that time this method will not work.
Ex:
samplefile

#This is a sample file
#Only for examples

ou=x,o=n
ou=a,ou=b,o=c

o=n

So, now we need to pass 3 arguments like this:
sample.sh ou=x,o=n ou=a,ou=b,o=c o=n
# 4  
Old 02-13-2012
MySQL

below script will ignore # and blank space

Code:
#! /bin/bash
val=`awk '$0 !~ /^[#]|[\ ]/{
i=0
printf("%s ",$0)
}' inp4`
./te5 $val
echo $val

if you want add any other pattern apart from # and blank space you can update this line in the script
Code:
awk '$0 !~ /^[#]|[\ ]/{

Thanks
Kalai
This User Gave Thanks to kalpeer For This Post:
# 5  
Old 02-13-2012
Why not let the shell script parse the configuration file?
# 6  
Old 02-13-2012
@kalpeer, thanks a lot dude. Can you please clarify how it works? It will be helpful for me in later contexts as I am new to linux shell script area.Also I need to replace dot in any line by a comma. Sorry for bugging you Smilie. Going mad

@fpmurphy: Didn't get you. Can you please calrify?

Last edited by saurabhkoar; 02-13-2012 at 10:43 AM..
# 7  
Old 02-13-2012
Quote:
Originally Posted by saurabhkoar
@fpmurphy: Didn't get you. Can you please calrify?
Shell scripts can read shell scripts natively.

Code:
. configurationfile

No parsing, no nonsense with eval, it just reads it, and it just works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass arguments to an sftp script??

hi, Is it possible to pass arguments to a sftp script and use those arguments in the program? for example sftp_script FILENAME=$1 #!/usr/bin/expect spawn /usr/bin/sftp abc@ftp.abc.com expect "abc@ftp.abc.com's password:" send "pass\r" expect "sftp>" send "mput $FILENAME\r"... (9 Replies)
Discussion started by: Little
9 Replies

2. UNIX for Dummies Questions & Answers

Pass arguments to the library .so

Hello, Please, how can i pass arguments to my lib.so ? my lib.so is written in c and i need some arguments in the code .. LD_PRELOAD=lib.so ./program Thank you. (1 Reply)
Discussion started by: chercheur857
1 Replies

3. Shell Programming and Scripting

To pass arguments to makefile using script

Hi, I want to run a target of makfile using script by passing different arguments to it again n again. I i need to grep certain things from the log file. eg make abc KAB=8 BAC=8 >& KAB_BAC.log grep "timeA" KAB_BAC.log grep "timeB" KAB_BAC.log (i want to store the difference of the two time... (0 Replies)
Discussion started by: vdhingra123
0 Replies

4. Shell Programming and Scripting

[C SHELL] How to pass dynamic number of arguments

I have a task. The scenario is like this. I have a operation program (OPR1) , whose function is to simply double the (single)value it receives as input. I have to write a script to operate the OPR1 and save its output in a file. Later, I have to modify the script so as to be able to operate ... (0 Replies)
Discussion started by: animesharma
0 Replies

5. Shell Programming and Scripting

pass arguments unchanged

Hi, I have to use ksh on HP-UX for some scripting. I usually use "set -e -u" in scripts to stop if errors occur or a typo is in a variable name. Now I try to use "$@" to pass the arguments unchanged to another function, which works without problems - unless I try to call the script without... (7 Replies)
Discussion started by: michas
7 Replies

6. Shell Programming and Scripting

Deleting both hidden and regular files in a single shot

How to delete both hidden (. dot files) and regular files available in a directory in a single shot (just one pass instead of two rm commands)? The following are not helping: rm .* (or) rm \.* (or) rm .??* (or) rm * All these commands delete either hidden or regular files at one... (4 Replies)
Discussion started by: royalibrahim
4 Replies

7. Shell Programming and Scripting

How do we pass multiple arguments into awk

How do we pass multiple arguments into awk : name=john age=12 now i have to pass both age and name into awk.. how to do it? like : awk -v var=... (4 Replies)
Discussion started by: abhinav192
4 Replies

8. Shell Programming and Scripting

need help to pass arguments in script

Hi, I have a my script here-- print "The Perl Script does the User health check and system health check...\n"; print "---------------------------------------------------------------------\n"; # use strict; my($OS); $OS = $^O; # need to test @ARGV before GetOptions shifts it if (@ARGV... (1 Reply)
Discussion started by: namishtiwari
1 Replies

9. UNIX for Advanced & Expert Users

Pass Kill with arguments

Dude, I want to kill a process, but the processid is in a text file. I have to read the text file for the process id and pass it as parameter to the kill command. Example $ cat prcid.txt 18650 I want to pass the value 18650 as a process id to kill command. $ kill -9 <value read from... (4 Replies)
Discussion started by: aksmuralee
4 Replies

10. Shell Programming and Scripting

pass arguments to called program

Thank you very much. (2 Replies)
Discussion started by: ShellUser
2 Replies
Login or Register to Ask a Question