The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
grep unix.com with google



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.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-24-2009
Registered User
 

Join Date: Jul 2009
Posts: 2
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
  #2 (permalink)  
Old 11-24-2009
Registered User
 

Join Date: Nov 2009
Posts: 2
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..
  #3 (permalink)  
Old 12-05-2009
Registered User
 

Join Date: Dec 2009
Posts: 22
Quote:
Originally Posted by rajiv1625 View Post
if you want to get all arguments in one go use $0.
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"

  #4 (permalink)  
Old 12-06-2009
Registered User
 

Join Date: Nov 2009
Posts: 2
sorry , it was a mistake.

Last edited by rajiv1625; 12-07-2009 at 02:00 AM..
Sponsored Links
Reply

Bookmarks

Tags
here-documents, interactive, non-interactive

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0