Say I had an extremely simple script called testScript.sh:
and I invoked it as:
or
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?
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.
Being called to get "$1"...
Results on OSX 10.12.1, default bash terminal...
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.
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
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":
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:
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:
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:
Call this with:
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 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)
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)
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)
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)
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)
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)
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)
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)
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)
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)