How to use stdin as argument for script?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to use stdin as argument for script?
# 1  
Old 12-05-2016
Question How to use stdin as argument for script?

Say I had an extremely simple script called testScript.sh:

Code:
#!/bin/sh
echo $1

and I invoked it as:
Code:
source testScript.sh <<< x

or
Code:
source testScript.sh <<< inputFile.txt

When I do the above the values don't appear in the echo statement, and I know that is because in the echo statement I am referring to an argument.

How can I get the value from stdin to be used as an argument to be used in the script. Or how can I invoke it as above and use the stdin value or values in the script?

Most all of the solutions I've looked up use the constructs "read", "exec" or "/dev/stdin" as a solution but in this scenario how would I do it without using the above solutions? How can I use the values passed in from stdin?

Thanks.
# 2  
Old 12-05-2016
Hi,

You mean like this ?

Code:
cat test.sh
echo $1

./test.sh x
x

or
Code:
./test.sh <<< echo "x"

# 3  
Old 12-05-2016
Is this what you are after:-
This is not 'source'd as such as source serves no purpose here.
Also here strings and the word 'source' are undefined inside a POSIX shell.
Code:
#!/bin/sh
# source_test.sh
echo "$1"

Being called to get "$1"...
Code:
#!/bin/sh
# source_call.sh
y=$( ./source_test.sh "AMIGA A1200..." )
echo "Looking for computer, $y"

Results on OSX 10.12.1, default bash terminal...
Code:
Last login: Mon Dec  5 22:00:27 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> chmod 755 source_test.sh
AMIGA:amiga~/Desktop/Code/Shell> chmod 755 source_call.sh
AMIGA:amiga~/Desktop/Code/Shell> ./source_call.sh
Looking for computer, AMIGA A1200...
AMIGA:amiga~/Desktop/Code/Shell> _

# 4  
Old 12-06-2016
Why would you use stdin as an argument to the script? stdin by default is available to the script UNLESS explicitly closed. Use redirection to process files or other streams in your script.
# 5  
Old 12-06-2016
Code:
echo asdf | xargs ./myscript.sh

# 6  
Old 12-06-2016
Quote:
Originally Posted by Corona688
Code:
echo asdf | xargs ./myscript.sh

Sorry to say that but i think this example is badly constructed: what you do is not to feed the shell scripts <stdin> but to use xargs to transform xargs <stdin> to (positional) arguments on the scripts commandline. In fact, your example amounts to the same as issuing

Code:
./myscript.sh asdf

from the beginning.

Back to the original question of the thread: the confusion is about what constitues "input" to a process (or, in this case, a script).

Let us start with a picture: think of a process like a garden hose: you pour something in on top (stdin) and something comes out at the bottom (stdout). There is also a second outlet to this hose (stderr), but let us ignore that for now.

In this picture the pipe symbol is like a connector: you connect the bottom of one hose with the top of the other. Now, normal garden hoses just let out what goes in, but UNIX processes act differently: they transform what is running through them. To stay in the picture: say, one hose will turn red water into green water, another will turn water of any colour into oil of the same colour. If you need green oil but only have red water, you pour it through the "red-water-to-green-water"-hose first and what comes out of through the "water-to-oil"-hose":

Code:
<red-water-generating process> | rw2gw | w2oil

Commandline options in this picture are the control panel each of these hoses has: some knobs to steer its function, but all this is separate from the data that flows through this process. Your stereo system might have some controls (for volume, treble, basses, ...) to influence the way how what you play is played, but as long as you do not change the CD you always hear the same music.

Now, after this long introduction, what does your script: $1 ($2, $3, ...) are placeholders for so-called positional parameters: commandline arguments you provide when you call the script. Consider the following command:

Code:
./foo.sh abc def "xy zz" 123

When this is executed the shell interprets the command, separating "words" which are delimited by spaces (with the notable exception of "xy zz" because of the quotation marks, which counts as one word) and each of these "words" are represented by "$n" inside the script:

Code:
$1: abc
$2: def
$3: xy zz
$4: 123

But all this is just putting different control knobs onto the control panel of the script. Let us process now data - the stuff that flows through the hose:

Code:
#! /bin/ksh

while read LINE ; do
     echo "+++ $LINE +++"
done

exit 0

Call this with:

Code:
./script.sh < /some/inputfile

And you will see the content of /some/inputfile eclosed in some plus characters.

How does this work? The command read <variable> will read from <stdin> and assign a read line to a variable (in this case "$LINE"). As long is input is provided, read will return a 0 and the while-loop will continue. Once there is no moe input read will return non-0 and the while-loop will exit.

Inside the loop. as long as it is running, we simply output the line we have just read and decorate it with the plus signs (just to show that we have been there and really have processed it).

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with the Script to pass stdin in run time

I have put a script inside bash_profile of user "root". That script executes when we do "sudo su -" and prompts with a question : "Why are you logginf as root?" and users have to pass the reason then they get prompt. Inside script we have used "read -p input" to take input from user. I am a... (3 Replies)
Discussion started by: shekhar_4_u
3 Replies

2. Shell Programming and Scripting

Accept data from file or stdin in script

I have a script that looks like this:sed -f myfile.sed $1 > $1.out called myscript and would like to change it so the parameter isn't necessary: ls *.idx | myscript | xargs some_command What do I need to add so it can run either way? TIA ---------- Post updated at 09:41 AM ----------... (1 Reply)
Discussion started by: wbport
1 Replies

3. Shell Programming and Scripting

Passing stdin value into a script that is called from another script

I'm trying to automatically pass user input values into a script that is being called from another script, below is my current script and I added a comment next to the script where it asks user to enter input value. Thanks, mbak #!/bin/ksh echo " Adding disks for DB server then Enter YES... (2 Replies)
Discussion started by: mbak
2 Replies

4. Shell Programming and Scripting

Suplying stdin input within script

Hi , I have script in that , i uninstall rpm using rpm -ef $rc1 now my query is rpm -ef is asking user input DO YOU Want To continue (YES/NO) for each uninstalltion. now i want to supply YES variable when it asks for above statement . so that i dont have to give user input from... (4 Replies)
Discussion started by: raghavendra.nsn
4 Replies

5. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

6. Shell Programming and Scripting

script hangs when reading from stdin

script: while read inputline; do if ; then if ; then break fi fi done Looks like the script hangs when stdin is empty or contains space. Any ideas on how to circumvent this? is it possible to use getline to process stdin content? (4 Replies)
Discussion started by: ux4me
4 Replies

7. Shell Programming and Scripting

Shell script to pass multiple stdin to prorgam?

Running on AIX 5.3L. I have a program "foo" written in Fortran that requires 3 levels of inputs from stdin (command prompt). > foo Enter Input 1: a Enter Input 2: b Enter Input 3: c running foo success! > How do I get a shell script to run this automatically? > echo "a" | foo... (2 Replies)
Discussion started by: discoganya
2 Replies

8. Shell Programming and Scripting

reading from stdin in a shell script

Hello, I've managed to get my .procmailrc file to work. At least it triggers a script which creates a file. But the file is empty. How do I get at the data that's been piped? I've done much creative googling to no avail. I belive it should be in stdin, but I can't figure out how to access... (4 Replies)
Discussion started by: mmesford
4 Replies

9. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

10. Shell Programming and Scripting

Cannot redirect to STDIN in a shell script

I am unable to use STDIn redirection with < (commands) When I do the following, both approaches work and give the same results: 1. $ printf "aaa\nbbb\n" > file1 $ printf "111\n222\n" > file2 $ cat file1 file2 aaa bbb 111 2222. $ cat <(printf "aaa\nbbb\n") <(printf "111\n222\n") aaa... (8 Replies)
Discussion started by: metaltree
8 Replies
Login or Register to Ask a Question