![]() |
|
|
grep unix.com with google
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Our Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
How to pass arguments to an interactive script
Hey guys, I have an interactive script that is quite critical to our production environmentl thus updating it to run non-interactively is not an option. The script takes a varying number of arguments, which it ques untill user confirm end of data entry e.g of user input [arguments]: Code:
1 2 y #add another arguments set 3 4 n #this is last set of arguments y #start processing the arguments to be passed to the script are in a file. say file.in i tried using a here-document, but it turns out that you can't put a here-document in a file as i kept getting "terminator not found" error message from ksh i wanted to run the script like this: script << here_doc where here_doc had: Code:
$ arguments $ it didn't work! as explained above. now the funny part, if i pass the arguments through a pipe to the script standard input, it works when using echo or print to print the arguments to standard output: i.e. [Korn shell] Code:
echo "1\n2\nn\ny" | myscript ^^ this works perfectly, my script runs and reads its input from the pipe. print also works as above but my problem is, i have a file of these arguments that i want the script to sequentially read its input from. if I use cat, it doesn't not work!!! the script reads NULL in the first variable!! Code:
cat file.in | myscript the same also happens when I do myscript < file.in #script reads NULL in first variable by the way, the script that is trying to run this script is on another machine so it ssh-es to the host machine and run the script. But i don't think this has anything to do with all this. e.g. Code:
ssh me@remotehost 'myscript' Can anybody please suggest something to get myscript to read args from a file? Thanks in advance |
|
|||
|
Hi,
you can pass file name as a argument to Your shell script like this $ script.sh filename in shell script arguments are stored into special variables i.e first argument is stored into $1,second in $2 .....if you want to get all arguments in one go use $*. so inside the script use : $ filename=$1 #to get the file name. Then use for loop to get the contents of file like this.. for i in `cat $filename` do firstline=$i your logic.......... done Note : if the file location is differ then script location the use relative path name as : $ script.sh /home/rajiv/filename Last edited by rajiv1625; 12-07-2009 at 02:01 AM.. |
|
|||
|
I am new to the forum so I was browsing some of the old threads, I noticed this post and unless this works differently in the shell you are using the above quote is incorrect. Maybe it was just an oversight. $0 refers to the name of the command itself, $* will provide all arguments at once. See below: Code:
$ ./foo.sh 1 2 3 4 5 This was the dollar 1: 1 This was the dollar 2: 2 This was the dollar 3: 3 This was the dollar *: 1 2 3 4 5 This was the dollar 0: ./foo.sh $ cat foo.sh #!/bin/sh echo "This was the dollar 1: $1" echo "This was the dollar 2: $2" echo "This was the dollar 3: $3" echo "This was the dollar *: $*" echo "This was the dollar 0: $0" |
|
|||
|
sorry , it was a mistake.
Last edited by rajiv1625; 12-07-2009 at 02:00 AM.. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| here-documents, interactive, non-interactive |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do we pass multiple arguments into awk | abhinav192 | Shell Programming and Scripting | 4 | 11-18-2009 10:08 AM |
| need help to pass arguments in script | namishtiwari | Shell Programming and Scripting | 1 | 06-02-2009 07:26 AM |
| Need help to pass arguments to shell script | bhargav20 | Shell Programming and Scripting | 1 | 05-18-2009 05:10 AM |
| Pass Kill with arguments | aksmuralee | UNIX for Advanced & Expert Users | 4 | 04-25-2008 06:07 AM |
| How to pass arguments to a function in a shell script? | preetikate | Shell Programming and Scripting | 3 | 03-01-2004 04:55 AM |