I'd like to do something like that:
cat file.txt | ./myscript.sh
file.txt
http://google.com
http://amazon.com
...
How can I read data in myscript.sh?
A pipe in Bash takes the standard output of one process and passes it as standard input into another process. Bash scripts support positional arguments that can be passed in at the command line.
Just like && , || is a bash control operator: && means execute the statement which follows only if the preceding statement executed successfully (returned exit code zero). || means execute the statement which follows only if the preceding statement failed (returned a non-zero exit code).
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
You can do this with a while loop
(process line by line), this is the usual way for this kind of things :
#!/bin/bash
while read a; do
# something with "$a"
done
For further informations, see http://mywiki.wooledge.org/BashFAQ/001
If instead you'd like to slurp a whole file in a variable, try doing this :
#!/bin/bash
var="$(cat)"
echo "$var"
or
#!/bin/bash
var="$(</dev/stdin)"
echo "$var"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With