Using here document when passing arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using here document when passing arguments
# 1  
Old 07-20-2014
Question Using here document when passing arguments

I have a script test.sh which reads various inputs from a user.

Code:
#!/bin/ksh
read x
read y
read z

echo x: $x y: $y z: $z
# read few more things again

read a
read b

echo a: $a b: $b

When i run this script as
Code:
test.sh << EOF
1
2
EOF

Output:
Code:
x: 1 y: 2 z:
a: b:

it doesn't ask to input for remaining read statements and auto-assigns empty values to remaining variables. however if i give 5 rows or more than 5 rows, it assigns first 5 values to the 5 variables:
Code:
test.sh << EOF
1
2
3
a
b
xyz
abc
EOF

Output:
Code:
x: 1 y: 2 z: 3
a: a b: b

Works fine here and i guess extra inputs are just ignored.

Shouldn't it wait to ask for user input for remaining variables if its not provided? Am i using it in a right way?

Last edited by bartus11; 07-20-2014 at 06:16 AM.. Reason: Please use [code][/code] tags.
# 2  
Old 07-20-2014
This is expected behavior. If there is too much input then the last lines never get read. If there is no more input on stdin then the other values get empty values. It is up to the script to provide additional checks..

One approach may be to provide an alternative source if the first one dries up. For example:

Code:
#!/bin/ksh
read x || read x < /dev/tty
read y || read y < /dev/tty
read z || read z < /dev/tty

echo x: $x y: $y z: $z
# read few more things again

read a || read a < /dev/tty
read b || read b < /dev/tty

echo a: $a b: $b


or

Code:
#!/bin/ksh
{
read x || read x <&3
read y || read y <&3
read z || read z <&3

echo x: $x y: $y z: $z
# read few more things again

read a || read a <&3
read b || read b <&3

echo a: $a b: $b
} 3</dev/tty


Last edited by Scrutinizer; 07-20-2014 at 09:49 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-21-2014
Thanks both work fine and good enough for me.
In the second example, what kind of redirection is <&3 and then 3< ?
# 4  
Old 07-21-2014
In the command:
Code:
read b || read b <&3

The first read reads data from the standard input of the script. If that fails (presumably because we have hit EOF on standard input), the second read uses <&3 to redirect whatever file is connected to file descriptor #3 to its standard input.


Code:
{
commands
} 3</dev/tty

opens /dev/tty for input and connects it to file descriptor #3 of all of the commands in that block.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing arguments to interactive program through bash script, here document

Dear Users, I have installed a standalone program to do multiple sequence alignment which takes user parameters to run the program. I have multiple sequence files and want to automate this process through a bash script. I have tried to write a small bash code but its throwing errors. Kindly... (13 Replies)
Discussion started by: biochemist
13 Replies

2. Shell Programming and Scripting

Passing arguments that contain space

hi All, i am trying to pass arguments that contain space , value will be stored in variables to be used further in script , i went thru previous posting , still its not clear to how to implement for my case. passing 3 args test.sh it is 'fun to work in unix' inside shell ... (3 Replies)
Discussion started by: gvkk
3 Replies

3. Shell Programming and Scripting

Passing arguments--Error

Hi, i have a file.txt with data Bangalore Chennai Hyd filename of the script is: new.sh result=`cat file.txt | grep $1` if then echo pass else echo fail fi i am executing the file in the cmd line as "sh new.sh Bangalore" o/p is pass if i give "sh new.sh delhi" o/p is... (6 Replies)
Discussion started by: harsha85
6 Replies

4. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

5. Programming

Passing arguments to shellcode

Is there any way I could pass arguments to shellcode. My goal is to store a program in a image file, and have another program read and run the code with arguments in memory. Currently I can store a program in a image file, then read it back to the hard-drive run it normally then delete it when... (5 Replies)
Discussion started by: image28
5 Replies

6. Shell Programming and Scripting

Passing arguments at runtime

Hi .. Can any one please tell how to pass argument to shell script at runtime? I want to implement funcnality just like bc, where we can provide input while script is running and can be used later in the same script. Thanks in advance... (1 Reply)
Discussion started by: kunjalhg
1 Replies

7. UNIX for Dummies Questions & Answers

Passing arguments

I need to pass arguments to a shell script.My batch is calling some java program. ################# x=$1 y=$2 java -classpath program ################### if first parameter and second parameter is null then java -classpath program if first parameter is not null and second parameter is... (3 Replies)
Discussion started by: mnjx
3 Replies

8. Shell Programming and Scripting

passing arguments

Hi I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like ./abc def /file <directory location> In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Discussion started by: zmfcat1
4 Replies

9. Shell Programming and Scripting

Passing Arguments-Help

Hi, I have a script which adds the user credentials to an ldap server. Im passing the variables as below.. /path/my_script $uname $pwd $environ ${deposit} If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Discussion started by: Tuxidow
5 Replies

10. UNIX for Dummies Questions & Answers

passing arguments

I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts: #!/bin/ksh read fname #if (( $# > 0 )); then $fname | ls -l #fi this produces a long listing of all the files in my current... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question