1

Trying to use the "read command" to accept user input from the command prompt itself but my script doesnt seem to be moving forward

echo "Do you want to continue?(yes/no)" read -p $1 if [ "$1" == "yes" ] then sleep 5s echo "" echo " move ahead" else echo "" echo "Skipping The Step.." echo "" sleep 5s fi 

I want to execute the script like this..

sh script.sh yes sh script.sh no 

Added a -p to the above script and all seems to work very well. This is my real problem. I have another script test.sh which calls script.sh. So this is how i put the input

cat test.sh yes #!/bin/bash echo "execute the below script" sh script.sh $1 sh test.sh yes 

This way doesnt work as the script picks up a default no and moves ahead. Any ideas.

5
  • I added a -p command in front of the script and it seems to work. This is my real problem. I have another script called test.sh which in turn calls this script ie script.sh. So in test.sh i added this line. cat test.sh yes sh script.sh $1 This was it doesnt seem to work
    – Super
    CommentedJul 20, 2016 at 6:09
  • No, the -p argument doesn't even allow the user to change the answer.CommentedJul 20, 2016 at 6:10
  • Do you want it as a command line argument or as user input?CommentedJul 20, 2016 at 6:10
  • @JuliePelletier - looking at a command line input but only problem is one script has to call the other script like i showed it above.
    – Super
    CommentedJul 20, 2016 at 6:16
  • bash is not sh. bash is the GNU implementation of a sh interpreter, but there are many other different implementations. read -p is not valid standard sh syntax. Those different sh implementations will treat it differently. Check here for the sh language specification, and specifically for the read utility.CommentedJul 20, 2016 at 7:24

1 Answer 1

4

$1,$2 … - command line positional arguments and couldn't be assigned like read [-p] $1 or any other way except

set -- firsr_arg second_arg … 

For your case it can be test if arguments is present then test them

while [ -z "$REPLY" ] ; do if [ -z "$1" ] ; then read -p "Do you want to continue?(yes/no) " else REPLY=$1 set -- fi case $REPLY in [Yy]es) sleep 5s echo -e "\n move ahead" ;; [Nn]o) echo -e "\nSkipping The Step..\n" sleep 5s ;; *) echo "Wrong answer. Print 'yes' or 'no'" unset REPLY ;; esac done 
6
  • what set -- does ?
    – Rahul
    CommentedJul 20, 2016 at 6:42
  • That is the obvious answer but so far I doubt it will clarify the confusion in OP's mind.CommentedJul 20, 2016 at 6:42
  • 1
    @Rahul help set: Set or unset values of shell options and positional parameters. Change the value of shell attributes and positional parameters, or display the names and values of shell variables.
    – Costas
    CommentedJul 20, 2016 at 6:54
  • @Costas cool, you have already earned my upvote ;)
    – Rahul
    CommentedJul 20, 2016 at 6:59
  • That very much depends on the sh implementation. read 1 or 1=foo work in zsh. 1=foo also works in rc and derivatives. Note that read -p is not sh syntax. In some sh implementations, it's to read from the co-processes. Only the bash implementation of sh recognises it as the way to specify a prompt. In any case, none of read -p var, read 1 or read without variable is valid POSIX sh syntax. You want printf 'Prompt: '; read REPLY in a sh script.CommentedJul 20, 2016 at 7:21

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.