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

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

Related

Use SED to comment out cronjobs (not that simple)

I have a rather large BASH function that I'm working on. This function is a CronJob generator. This script is intended to be run with SUDO privileges, and will allow the user to inspect the unprivileged user's Crontab file. They can create new cronjobs (a few questions and it does the proper syntax for them), they can also remove a cronjob. That's where I've hit a wall.
In this part of my CASE statement, the user has been asked if they want to create a new cronjob -- they reply with "N" or "n" and we get here:
#!/bin/bash
read -r -p $'Would you like to create a new cronjob? [y/n]\n\n--> ' CRON
case "$CRON" in
y|Y)
echo "not pertinent to this discussion"
;;
n|N)
read -r -p $'\n\nWould you like to REMOVE a crontab entry? [y/n]: ' REMOVE
case "$REMOVE" in
Y|y)
declare -a CRONTAB
while IFS= read -r LINE
do
CRONTAB+=("$LINE")
done < <(grep -v '#' /var/spool/cron/"$SCRIPTUSER")
echo -en "\nPlease select a cronjob to remove from the Crontab file:\n\n"
PS3=$'\n\nPlease enter your selection: '
select LINE in "${CRONTAB[#]}"
do
echo "Going to remove \"$LINE\""
read -r -p $'Is this correct? [y/n]' CHOICE
case "$CHOICE" in
Y|y)
sed "s/$LINE/^#&/g" -i /var/spool/cron/"$SCRIPTUSER"
break
;;
N|n)
break
;;
esac
done
echo -en "\n\nCurrent Crontab entries for $SCRIPTUSER:\n\n"
echo -en "\n\n######################################\n\n$(grep -v '#' /var/spool/cron/"$SCRIPTUSER")\n\n######################################\n\n"
sleep 3
break
;;
N|n)
break
;;
esac
;;
esac
The problem I'm having is these are my cronjob entries I'm testing with:
The SED statement doesn't seem to be doing anything at all. I would imagine the '*' and '/' are probably messing with the SED pattern, and I have already tried a sed where I escaped all the '/' but it still passed over it like nothing was there.
I appreciate the extra set of eyeballs, thank you!

how to create and send output directly to printer using do shell script

I am trying to get a shell script that works in the sh shell to work as an applescript do shell script command. The script simply takes a series of elp2 commands and pipes them to lpr for printing. This works when typed directly into the shell, but when run in applescript it does not print. The printer is receiving data but it is not formated correctly so nothing prints. I believe the issue is with the way the quotes are escaped. Here is the code:
do shell script "{ echo N
echo OD10
echo q812
echo Q1218,24
echo D15
echo ZT
echo A70,40,0,5,3,3,N,\\'F\\'
echo A610,70,0,3,1,1,N,\\'U.S.\\'
echo A590,95,0,3,1,1,N,\\'POSTAGE\\'
echo A580,120,0,3,1,1,N,\\'REQUIRED\\'
echo A43,240,0,4,2,1,N,\\'USPS FIRST-CLASS MAIL\\'
echo A43,300,0,3,1,1,N,\\'Name\\'
echo A43,325,0,3,1,1,N,\\'street\\'
echo A43,350,0,3,1,1,N,\\'city, VA 12345\\'
echo A43,400,0,3,1,1,N,\\'ADDRESS SERVICE REQUESTED\\'
echo A140,490,0,4,1,1,N,\\'ship to\\'
echo A140,520,0,4,1,1,N,\\'company\\'
echo A140,550,0,4,1,1,N,\\'address\\'
echo A140,580,0,4,1,1,N,\\'city, state, 12345\\'
echo A140,610,0,4,1,1,N,\\'country\\'
echo A140,640,0,4,1,1,N,\\'\\'
echo LO10,10,760,4
echo LO10,210,760,2
echo LO10,275,760,2
echo LO10,750,760,10
echo LO10,1050,760,10
echo LO10,1185,760,4
echo LO10,10,4,1175
echo LO770,10,4,1175
echo LO210,10,2,200
echo LO540,45,200,2
echo LO540,45,2,125
echo LO540,170,200,2
echo LO740,45,2,125
echo P1
echo N;} | lpr -P Label -o raw"
One way you can check how to write something in applescript is to write the normal unescaped text into a text file. Then you read the file into applescript. What you see in the result field is how you should write it. You can then copy/paste the result into your applescript.
For example, if I make a text file on my desktop called myText.txt with this inside...
echo A70,40,0,5,3,3,N,\'F\'
echo A610,70,0,3,1,1,N,\'U.S.\'
Then I use this applescript...
set f to (path to desktop as text) & "myText.txt"
read file f
My result is...
"echo A70,40,0,5,3,3,N,\\'F\\'
echo A610,70,0,3,1,1,N,\\'U.S.\\'"
So give that technique a try with your entire code. Good luck.

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

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.

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