Reading arguments for a shell script from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading arguments for a shell script from file
# 15  
Old 01-11-2012
Quote:
Originally Posted by methyl
Should be
Code:
Site="sitename"

Thanks a mil Methyl. I found the error and fixed it. The envfile.sh was pointing to a different host than it was supposed to. Thanks for your time
---------- Post updated at 06:07 PM ---------- Previous update was at 05:57 PM ----------

Quote:
Originally Posted by Corona688
There is no difference between having them in a file and having them in your file, ergo there is a typo.

Methyl identified it above for you.
Cheers.. I looked it up and it seems really cool. I'm loving the idea of it.

Quote:
While executing the shell script using “dot space dot slash”, as shown below, it will execute the script in the current shell without forking a sub shell.
$ . ./scriptfile
In other words, this executes the commands specified in the scriptfile in the current shell, and prepares the environment for you..

Last edited by goddevil; 01-12-2012 at 11:04 AM..
# 16  
Old 01-11-2012
try this
Code:
xargs -i sh -c './shscript {}' < parameters.txt


Last edited by Franklin52; 01-12-2012 at 04:56 AM.. Reason: Please use code tags for code and data samples, thank you
This User Gave Thanks to tarun_agrawal For This Post:
# 17  
Old 01-12-2012
I have run into problem.
I am using the following code to read from parameters.txt
Code:
while read x y
do
    ./shscript $x $y
done < parameters.txt

Code:
$ cat parameters.txt
env value1
env value2
env value3
env value4
env value5
env value6
env value7
env value8

This works perfectly.But if i make a small tweak to the code and include a if condition inside the while loop, only the alternate arguments are being read. For e.g, if i use the following code:
Code:
while read x y
do
    ./shscript $x $y
if [$y -gt 0 ] ; then
 echo The execution was successful
else
 echo Execution failed
fi
done < parameters.txt

The arguments that are passed to the shell are as follows
Code:
./shscript env value1
./shscript env value3
./shscript env value5
./shscript env value7

Can someone tell me why the alternate arguments are being missed? Thanks
# 18  
Old 01-12-2012
Quote:
if [$y -gt 0 ] ; then
Typo in this line. Should be a space character after the [ character.
Also, this is an integer comparison. Is $y and integer?
# 19  
Old 01-12-2012
Quote:
Originally Posted by methyl
Typo in this line. Should be a space character after the [ character.
Also, this is an integer comparison. Is $y and integer?
Code:
if [ "$z" == "MX" ] ; then

I use the code above and the condition works.

I tried inserting a newline character in the file paramers.txt as below and the program works perfectly. I dont use any breaks in my if.. else command.. I dont want to have to insert a new line in the parameters.txt file. I cant see where the logic is going wrong. Smilie
Code:
env value1
 
env value2
 
env value3
 
env value4
 
env value5
 
env value6
 
env value7
 
env value8


The following is my code:
Code:
while read x y z
do
if [ "$z" == "GOD" ] ; then
./shell.sh $x $y
a= parse $y
echo bla bla
if [ $a -gt 0 ] ; then
echo "`read -p "user i/p"`"
else
echo Executing the value............
fi
fi
if [ "$z" == "DEVIL" ] ; then
./ggmu.sh
fi
done<parameters.txt

If i remove the bolded out code above, the script reads the arguments properly!! Why is that if condition screwing up the reading of the parameters.txt file?

Last edited by goddevil; 01-12-2012 at 02:54 PM..
# 20  
Old 01-12-2012
It's not the "if" condition, its the "read -p" (which was not mentioned in your previous post). It's in backticks so it still functions within the "echo" line.
There is only one STDIN channel and that "read -p" is eating data from you main read loop. Every alternate record. QED.


Ps. Please take care to post the script and data which matches the symptoms.
This User Gave Thanks to methyl For This Post:
# 21  
Old 01-12-2012
Quote:
Originally Posted by methyl
It's not the "if" condition, its the "read -p" (which was not mentioned in your previous post). It's in backticks so it still functions within the "echo" line.
There is only one STDIN channel and that "read -p" is eating data from you main read loop. Every alternate record. QED.


Ps. Please take care to post the script and data which matches the symptoms.
Ah yes.. I see it now. Thanks.

Is there i any way i can tweak the above code to allow user input without screwing up the file read?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing Arguments to shell script from file is not working as expected.

Hi All, I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination. # file_copy.sh source_dir = ${source_dir} target = ${target_dir} cp source_dir target and my parameter file is like below #parameter_file.txt source_dir =... (4 Replies)
Discussion started by: Narasimhasss
4 Replies

2. UNIX for Beginners Questions & Answers

Shell script file check throws [: too many arguments

While I am trying to check the filename/s in IF statement of a shell script (RedHat Linux 6) I am getting below error: File check: filename_time2=`date --date='yesterday' +%Y-%m-%d` cd /location/of/the/files/to/copy if then cp server.log-$filename_time2* ../archive/new... (5 Replies)
Discussion started by: Dip
5 Replies

3. Shell Programming and Scripting

php file unable to run shell script with arguments

echo $result=exec("./permit.sh".$_FILES); pls suggest some other method to run shell script in php .:wall::mad: (0 Replies)
Discussion started by: upvan111
0 Replies

4. Shell Programming and Scripting

Error while reading from a file in shell script

Hi All, I'm writing a script to read a file line by line and then perform awk function on it. I am getting an error . My file has one name in it "James". I'm expecting my o/p to be youareJamesbond James ./users.sh: line 7: =: command not found #script to read file line by line #adding... (5 Replies)
Discussion started by: Irishboy24
5 Replies

5. Shell Programming and Scripting

send arguments to a .exe file from a shell script

Folks , can anyone post a sample showing a way to parse a variable containing a string to a .exe file . Thanks Venu (2 Replies)
Discussion started by: venu
2 Replies

6. Shell Programming and Scripting

How to pass arguments to SQL file passed in shell script?

Hi, I am using SYBASE database. in my script i am connecting to DB via using isql. isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b0 -w3000 -h0 -s"|" -i${MYDIR}/ABC.sql -oXYZ.txt << FINSQL i am taking a ABC.sql file to use the queries written in it and storing the output in... (3 Replies)
Discussion started by: dazdseg
3 Replies

7. Shell Programming and Scripting

file reading through shell script

For reading a file through shell script I am using yhe code : while read line do echo $line done<data.txt It reads all the line of that file data.txt. Content of data.txt looks like: code=y sql=y total no of sql files=4 a.sql b.sql c.sql d.sql cpp=n c=y total no of c files=1 (4 Replies)
Discussion started by: Dip
4 Replies

8. Solaris

Passing arguments to a shell script from file while scheduling in cron

Hi, I have a shell script Scp_1.sh for which I have to pass 2 arguments to run. I have another script Scp_2.sh which in turns calls script Scp_1.sh inside. How do I make Scp_1.sh script to read arguments automatically from a file, while running Scp_2.sh? -- Weblogic Support (4 Replies)
Discussion started by: weblogicsupport
4 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

10. UNIX for Dummies Questions & Answers

Reading runtime arguments from a file

Hi All, I am running a script which wud take a long time to complete execution ... In between, it asks for yes/no sort of confirmation from user for doing some acivities .... Now that I dont want to wait till the script executes upto this point where it needs user confirmation, is there any way... (4 Replies)
Discussion started by: Sabari Nath S
4 Replies
Login or Register to Ask a Question