Getting bash script to enter in numbers in an 'interactive mode' prompt - linux

There is an interactive mode that can be turned on in the program ORCA I am using by typing in the following command:
module load openmpi/2.1.2 orca/orca_4_0_1_2_linux_x86-64_openmpi202
Once this is enabled, I can give it a command to plot a graph with:
orca_plot IPC_CAS1_restart2f-NEVPT2.gbw -i
The program then offers me choices, which I can choose from by entering a number into the prompt. I wish to automate this process by having a bash script that enters a specific sequence of numbers for me (i.e. 1, then 3, then 2, then 7 for example).
My script looks like the following,
#!/bin/bash
module load openmpi/2.1.2 orca/orca_4_0_1_2_linux_x86-64_openmpi202
orca_plot IPC_CAS1_restart2f-NEVPT2.gbw -i
1
3
2
7
I get the messages
"line 4: 1: command not found", "line 5: 3: "command not found", "line 7: 2: command not found", "line 8: 7: command not found".
How can I fix this?

You need to convert those lines in the script into input to orca_plot. Use a heredoc:
#!/bin/bash
module load openmpi/2.1.2 orca/orca_4_0_1_2_linux_x86-64_openmpi202
orca_plot IPC_CAS1_restart2f-NEVPT2.gbw -i << EOF
1
3
2
7
EOF

Related

Stuck with While loop in WSL2

I am in the very basic of the basic with Shell. In general, I only use the WSL2 for ssh. Now, I write a loop, so I google for an example to see how it works. Here is my ref:https://ryanstutorials.net/bash-scripting-tutorial/bash-loops.php. The problem is that even if I just copy and paste their examples, I get the error:
loop_learning.sh: line 4: $'\r': command not found
loop_learning.sh: line 10: syntax error near unexpected token `done'
loop_learning.sh: line 10: `done'
This is the code that I just copy and paste:
#!/bin/bash
# Basic while loop
counter=1
while [ $counter -le 10 ]
do
echo $counter
((counter++))
done
echo All done
The problem is probably that you copy-pasted the script from Windows and tried to execute it from Linux. Windows uses CR in addition to NL for new lines, where Linux uses only '\n' and finds the former ('\r') strange.
Try something like this from your WSL2 terminal:
sed 's/\r//g' your-copied-script.sh > your-clean-script.sh
And execute your-clean-script.sh

How to compare user input with array and execute command?

I am trying to write a script to login into different systems by providing system name as input and making it variable by read option. However when i try to compare it with defined Array it's throwing me error and stating command not found.
Succeeded in making use input as variable but not able to compare it properly with defined array.
Below is the code i have written.
#!/bin/bash
cluster=("namico1c.mylabserver.com","namico2c.mylabserver.com")
echo "Please enter a Cluster Name to login: "
read clname
for item in ${cluster[#]};do
echo ${item};
if ["${clname}"="${item}"]; then
ssh test#$clname
else
echo "Cluster is not correct"
fi
done
[test#namico3c ~]$ ./test.sh
Please enter a Cluster Name to login:
namico1c.mylabserver.com
namico1c.mylabserver.com,namico2c.mylabserver.com
./test.sh: line 7: [namico1c.mylabserver.com=namico1c.mylabserver.com,namico2c.mylabserver.com]: command not found
Cluster is not correct
alternative:
#!/bin/bash
cluster=("namico1c.mylabserver.com" "namico2c.mylabserver.com")
select clname in "${cluster[#]}"; do
ssh test#$clname
break
done

Shell programming error [duplicate]

This question already has answers here:
Syntax error near unexpected token 'then'
(2 answers)
Closed 7 years ago.
I'm new to shell programming and I'm supposed to do this
Create two directories OS_filesR and OS_filesW on Desktop
Ask the user to enter file name.
Create file with the entered file name in OS_filesR if this is the Odd Creation and
remove readable permission.
If this is the even Creation, Create the file in OS_filesW and remove writable
permission.
Ask user if he/she wants to create another file if yes repeat steps (2, 3), if no
exist.
Here is the code:
mkdir /home/karim/Desktop/OS_filesR /home/karim/Desktop/OS_filesW
counter=0
while(1)
do
echo "Enter the file name"
read var
if[$counter % 2 -eq 0]
then
touch /home/karim/Desktop/OS_filesW/$var
chmod -w $var
else
touch /home/karim/Desktop/OS_filesR/$var
chmod -r $var
fi
echo "Do you want to create another file? Enter yes or no"
read var2
if[$var2 != "yes"]
then
break
fi
counter++
done
I keep getting this error:
line 9: syntax error near unexpected token then'
line 9: then'
So how can I fix this?
Place a space after the "[" on line 9. "[" is just an alias for the test command, and is not parsed separately from the rest of the string without the space.

bashscript Heredoc + FTP error

I try to do this
#!/bin/bash
ftp "$HOST"$3"/"$2"/" <<EOD
#toggle Interactive mode
prompt off
lcd $5"/"$4
mget "$4"*
exit
EOD
I get the following error
syntax error: unexpected end of file
When I changed it to or any other possibility
ftp "$HOST"$3"/"$2"/" <<<EOD
#toggle Interactive mode
prompt off
lcd $5"/"$4
mget "$4"*
exit
EOD
I get
./download.sh: line 31: 87621 Segmentation fault: 11 ftp "$HOST"$3"/"$2"/" <<< EOD
./download.sh: line 20: prompt: command not found
./download.sh: line 21: lcd: command not found
./download.sh: line 22: mget: command not found
I am not sure how to fix this. What am I supposed to doooo O_O
On my Mac, the segmentation faults were produced by the comments in the script. Removing the lines with the trailing # would make it work.
many interactive commands don't really deal well with piped input. maybe try http://www.columbia.edu/kermit/ftpscripts.html ?
that said, many others seem to have had success doing what you're doing (e.g. http://www.unix.com/unix-advanced-expert-users/4189-automated-ftp.html ), so maybe you just have a quoting problem? try changing the command (ftp "$HOST"$3"/"$2"/") to just cat to see if the shell is properly passing there here-doc to it?
this, too, may lend insight if you end up needing to supply a password: http://www.stratigery.com/scripting.ftp.html

bash select command

I'm trying to run a script that uses the select command and I get error below. I'm running the most recent version of ubuntu. Why does it say the commands are not found?
#!/bin/bash
# Scriptname: runit
PS3= "Select a program to execute: "
select program in 'ls -F' pwd date cal exit
do
$program
done
This is the output:
runit.sh: 3: Select a program to execute: : not found
runit.sh: 4: select: not found
runit.sh: 5: Syntax error: "do" unexpected
Delete the space after the equal sign:
PS3= "Select a program to execute: "
^

Resources