1

If I have two read commands in the script and I have to run in nohup mode, then how can I give values using echo command.

Eg. script:

#!/bin/sh 
echo "/n read the value1" 
read info1 
echo "/n read the value2" 
read info2
 echo "$info1 $info2">>log.txt 

I have to run this is nohup mode then how can I provide the values to read.

1 Answer 1

1

This is actually possible. You can do something like this:

nohup sh -c "printf 'foo\nbar\n' | myScript.sh"

That will pass foo and bar as input.


But don't do that. Just don't use read. In fact, I can't even think of any good reason to ever use read inside a script. I'm sure there are some edge cases where it is necessary, but you should avoid it whenever possible. Having your users type in data is cumbersome, prone to errors, and makes automating your script impossible.

So just change your script to read the data from the command line:

#!/bin/sh 
info1="$1"
info2="$2"
 echo "$info1 $info2">>log.txt 

Then, run your script with whatever you want as parameters:

myScript.sh foo bar

or

myScript.sh 'this one has spaces!' bar
1
  • A lot of learning in just 30s. Great answer. Thanks :) Commented Sep 14, 2019 at 17:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.