How do you get user input when running a bash script from a url? - bash

Consider this bash script :
#!/bin/bash
while true; do
read -p "Give me an answer ? y/n : " yn
case $yn in
[Yy]* ) answer=true ; break;;
[Nn]* ) answer=false ; break;;
* ) echo "Please answer yes or no.";;
esac
done
if $answer
then
echo "Doing something as you answered yes"
else
echo "Not doing anything as you answered no"
fi
When run from the command line using :
$ ./script-name.sh
It works just as expected with the script waiting for you to answer y or n.
However when I upload to a url and attempt to run it using :
$ curl http://path.to/script-name.sh | bash
I get stuck in a permanent loop with the script saying Please answer yes or no. Apparently the script is receiving some sort of input other than y or n.
Why is this? And more importantly how can I achieve user input from a bash script called from a url?

Perhaps use an explicit local redirect:
read answer < /dev/tty

You can run it like this:
bash -c "$(curl -s http://path.to/script-name.sh)"
Since you're supplying content of bash script to bash interpreter. Use curl -s for silent execution.

Related

How do I pass in arguments non-interactive into a bash file that uses "read"?

I have the following shell script:
#! /bin/bash
. shell_functions/commonShellFunctions.sh
. shell_functions/settings.sh
_valid_url=1
echo "Welcome to HATS Accessibility Testing Tool!"
echo "We recommend using Chrome browser for the best experience."
echo "What would you like to scan today?"
options=("sitemap file containing links" "website")
select opt in "${options[#]}"
do
case $opt in
"sitemap file containing links")
scanType="sitemap"
crawler=crawlSitemap
prompt_message="Please enter URL to sitemap: "
break;;
"website")
prompt_website
break;;
"exit")
exit;;
*)
echo "Invalid option $REPLY";;
esac
done
read -p "$prompt_message" page
echo $page
It was meant to prompt the user, however I wish to use the script in a CI setting where I pass the arguments through the console without prompting.
I'm currently using echo "<ARG1>\n<ARG2>" | bash run.sh, but I'm wondering if there's a better way to do this.
Use a here-document
./run.sh <<EOF
arg1
arg2
EOF

Bash file script escaping user input

This bash script when run on Mac terminal, it needs to ask for user input, then it needs to check if a a string "PLACEHOLDER_BACKEND_NAME="user-input" exists in a given file and if not it should exit the script.
echo -e "${YELLOW}enter app name${WHITE}"
read name
line=grep $name /path/to/file/entrypoint.sh
if [[ line != "PLACEHOLDER_BACKEND_NAME=\"$name\"" ]] ; then
exit 1
fi
It needs much tuning as I am not very familiar with bash scripts. any suggestions? thx
Your code needs a little tweaking:
echo -e "${YELLOW}enter app name${WHITE}"
read -r name
if ! grep -q PLACEHOLDER_BACKEND_NAME="\"$name\"" /path/to/file/entrypoint.sh; then
exit 1
fi

Ksh syntax error '=~'

read -p "The Process running for "$days" days continuously OK to kill this process (y/N)? " -u 4 ok
[[ "${ok}" =~ y ]] || continue
echo "Killing $pid"
kill -HUP "$pid"
fi
This is the snippet of my script ,when i am executing this it shows like
`=~' is not expected.
How to resolve it?
I'm guessing your shebang line has #!/bin/sh and so you don't have access to the full ksh syntax. If you do, ksh93 does appear to support [[ string =~ regex ]] syntax, so there's something here which doesn't add up right.
Either way, there is a construct which works just as well in classic Bourne shell which you can use instead, with the added bonus that your script will be compatible to systems where ksh is not available.
You use read -p <prompt> but that is a Bashism; the -p option to read has a quite different meaning in ksh93.
printf 'Process ran for %i days continuously, OK to kill this? ' "$days"
read -u 4 ok
case $ok in [Yy]* ) ;; *) continue ;; esac
echo "Killing $pid"
kill -HUP "$pid"
Your code looked for y anywhere in the input but I restricted that to only examine the first character.
(Your code had erratic indentation and an unpaired fi which I omitted.)
Your ? is 'how to resolve' - tripleee's suggestion looks like a solution - simplify the code - try:
if [[ "${ok} == "y" ]]
I tried copying your code snippet and I get a different error. Time for D&C - simple ksh93 script testing '=~'

While true Breaking Bash

I've been writing some bash scripts and for some reason, what used to be working code is no longer working. I had not done any changes, but the while true; do now breaks my bash script.
Here's part of it. Catting README.txt works flawlessly. The terminal echoes "Test..." correctly, but as soon as it hits "while true" it closes, regardless of the $exec bash that keeps it open after finishing.
#!/bin/bash
cat README.txt
echo
echo
sleep 2
echo
echo
echo "Test..."
sleep 3
#The option to read the README.txt file above
while true; do
read -p "Now is your chance to go back and read.
Or press y/n to continue." yn
case $yn in
[Yy]* ) break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
$exec bash
Any help would be greatly appreciated as I'm beginning to think it might be more than a code error.
UPDATE
After running the script by using "bash -x 111.sh", the error message I receive is
+ echo Test...
Test...
+ sleep 3
111.sh: line 59: syntax error near unexpected token `)'
111.sh: line 59: ` [Yy]* ) break;;'
How is there an unexpected token or a syntax error when many other sources say this is in fact correct, as well as it working prior to today.
Not sure why that all of a sudden doesn't work, but I replaced all instances of the while true loop with a read -p.
read -p 'Do you have (Program) installed? [y/n]' answer
case "${answer}" in
[yY]|[yY][eE][sS])
echo;;
[nN]|[nN][oO])
apt-get install (program); echo;;
esac
This seems to work much smoother and hasn't failed me as of yet.

How to read input from the user in a bash subshell [duplicate]

Consider this bash script :
#!/bin/bash
while true; do
read -p "Give me an answer ? y/n : " yn
case $yn in
[Yy]* ) answer=true ; break;;
[Nn]* ) answer=false ; break;;
* ) echo "Please answer yes or no.";;
esac
done
if $answer
then
echo "Doing something as you answered yes"
else
echo "Not doing anything as you answered no"
fi
When run from the command line using :
$ ./script-name.sh
It works just as expected with the script waiting for you to answer y or n.
However when I upload to a url and attempt to run it using :
$ curl http://path.to/script-name.sh | bash
I get stuck in a permanent loop with the script saying Please answer yes or no. Apparently the script is receiving some sort of input other than y or n.
Why is this? And more importantly how can I achieve user input from a bash script called from a url?
Perhaps use an explicit local redirect:
read answer < /dev/tty
You can run it like this:
bash -c "$(curl -s http://path.to/script-name.sh)"
Since you're supplying content of bash script to bash interpreter. Use curl -s for silent execution.

Resources