How to supply inputs for multiple prompts in one command line - bash

I have a shell script plan.sh which expects a user input (a/b) and then asks for another input (y/n).
I want to run this in one line like sh -a -y plan.sh which doesn't work.
I tried this too
./plan.sh << END_OF_INPUT
a
y
END_OF_INPUT
which throws an error:
/bin/bash: warning: here-document at line 0 delimited by end-of-file (wanted 'END_OF_INPUT')
Is there a way to do this better?

Related

how is the following UNIX command interpreted

How is this command interpreted:
cat file.txt |
If no arguments are passed after the pipe, can you explain at the C level why and how a new embedded command line interface is opened?
It's not the end of the command. The shell is waiting for the rest of it. If you force it, you get Syntax error: end of file unexpected

How to make a shell script take an argument passed in the command line?

I have a shell script (count_reviews.sh) that contains the following:
#!/bin/bash
grep -c "Author" "#1"
I have tried using "$1" as I found that this takes the first argument in the command line but I still get the error mentioned below. I have also used chmod +x to make my file executable.
The script counts the number of times "Author" appears in the file. I am required to be able to make the command line take an input of "% ./count_reviews.sh hotel_72572.dat" where hotel_72572.dat is an example file name. The number of times author appears will then be printed out underneath. When I do this however, I am getting an error -bash: fg: %: no such job. What is causing this and how do I fix it? My count_reviews.sh file is in the same directory as all of my hotel data files if that matters.
You should indeed use $1, but when you try to run your script just use ./your_script.sh your_argument don't add the % in the beginning.

: command not foundop/dominio.sh: line 2:

I'm writing shell scripting for Mac.
Here's my script:
echo "Bienvenido";
/Applications/sdk/platform-tools/adb devices;
sudo /Applications/sdk/platform-tools/adb shell input text 'sp.soporte#gmail.com';
It realize the correct operation, but here is the output :
$ /Users/julien/Desktop/dominio.sh
Bienvenido
: command not foundop/dominio.sh: line 1:
List of devices attached
4790057be1803096 device
: command not foundop/dominio.sh: line 2:
: command not foundop/dominio.sh: line 3:
julien$
If I erase the ; it's not working any more. How should I do????
I think you have Windows-style line endings in your script.
Unix-like systems, including MacOS, use a single LF character to terminate a line; Windows uses a CR-LF pair.
A Windows-style text file looks, on a Unix-like system, like ordinary text with an extra CR character at the end of each line.
Since you have a semicolon at the end of each line, this line:
echo "Bienvenido";
appears to the shell as two commands: echo "Bienvenido" and the CR character (which could actually be a command name if it existed). Note that the echo command was executed.
The shell prints an error message, something like:
/path/to/script: 1: CR: command not found
except that it prints the actual CR (carriage return) character, which moves the cursor to the beginning of the current line, overwriting part of the error message.
Translate your script to use Unix-style line endings. You can use dos2unix for this if you have it. (Read the man page; unlike most filter programs, it overwrites its input file by default.)
Incidentally, you don't need a semicolon on the end of each line of a shell script. Semicolons are needed only when you have multiple commands on one line.
Also, you should probably have a "shebang" as the first line of your script, either #!/bin/sh or #!/bin/bash (use the latter if your script uses bash-specific features).

Bash for loop error

I am trying out a simple bash script using for loop, and kept getting the following error:
'/test.sh: line 2: syntax error near unexpected token `do
'/test.sh: line 2: `do
The following is the code that is being used...
for animal in dog cat elephant
do
echo "There are ${animal}s.... "
done
However, when I tried on other machines.. it is working no problem.
Please help.
Your test.sh script has Windows-style line endings. The shell sees each \r\n sequence as a \r character at the end of the line.
The shell is seeing do\r rather than do. Since \r sends the cursor to the beginning of the line, that's why you're seeing the quotation mark at the beginning of the line. Try
./test.sh 2>&1 | cat -A
to see what's actually in the error message.
Filter your test.sh script through something like dos2unix to correct the line endings. (Be sure to read the man page first; dos2unix, unlike most text filters, overwrites its input file.)
This is a common problem on Cygwin. Did you use a Windows editor to create the script?

Can't get bash script to run, getting errors

I'm trying to run the script found here: http://blog.sebflipper.co.uk/2010/03/10/mysql-backup-as-separate-sql-files-with-rotation/comment-page-1/
bash /path/to/mysql-backup.sh
I'm getting the following errors:
/path/to/mysql-backup.sh: line 2:
: command not found
/path/to/mysql-backup.sh: line 4:
: command not found
/path/to/mysql-backup.sh: line 8:
: command not found
/path/to/mysql-backup.sh: line 10:
: command not found
/path/to/mysql-backup.sh: line 40: syntax error near unexpected token `{
'
/path/to/mysql-backup.sh: line 40: `function checkMysqlUp() {
Am I calling this command improperly?
Ok, it was the spaces, now I'm just getting the last 2 errors
Given the way the error messages are appearing, I think you downloaded the script with CRLF line endings and the shell is not liking this.
Use 'dos2unix' or 'dtou' or (if neither of the above is available, tr) to remove the carriage returns.
tr -d '\015' < /path/to/mysql-backup.sh > /path/to/other-mysql-backup.sh
Then try running:
/path/to/other-mysql-backup.sh
#! /bin/bash
This line at the top of the script isn't right. It should have no spaces.
It's not liking the blank lines in there. Are you sure when you maybe copied and pasted that you didn't inject ^M (carriage returns) or some other white-space character in there?

Resources