Bash: exit in script doesn't close terminal - linux

I'm working on a script to run on terminal like this one
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
yoooooooo
sleep 2
let COUNTER=COUNTER+1
done
exit
But once the counter reaches 9 and the while cycle stops, the terminal wont close with the command "exit".
Here's the output
pi#raspberrypi:~/Desktop $ ./sxdd
The counter is 0
./sxdd: line 7: yoooooooo: command not found
The counter is 1
./sxdd: line 7: yoooooooo: command not found
The counter is 2
./sxdd: line 7: yoooooooo: command not found
The counter is 3
./sxdd: line 7: yoooooooo: command not found
The counter is 4
./sxdd: line 7: yoooooooo: command not found
The counter is 5
./sxdd: line 7: yoooooooo: command not found
The counter is 6
./sxdd: line 7: yoooooooo: command not found
The counter is 7
./sxdd: line 7: yoooooooo: command not found
The counter is 8
./sxdd: line 7: yoooooooo: command not found
The counter is 9
./sxdd: line 7: yoooooooo: command not found
pi#raspberrypi:~/Desktop $
And it never quits... how do I fix that?

the terminal wont close with the command "exit"
That's because you run the exit command inside a script. There exit means "exit the script", not "exit the terminal".
If you want to close the terminal window from a script, you probably have to use commands like kill $pidOfTheTerminalWindow.
As an alternative, you can source the script, that is execute the script as if you typed it directely into the command line. Use either
source sxdd
or
. sxdd

Try to run your script like this:
exec ./sxdd
It replaces the shell process with your script and when the script exits your window will close.

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 ask for username and password with a shell script on linux and windows

I need to ask for username and password from a .sh script for linux and windows, how is this possible?
i tried this but it doesn't work:
#!/bin/bash
echo "Type the username, followed by [ENTER]:"
read username
echo "Type the password, followed by [ENTER]:"
read -s password
echo "The name entered was: $username"
echo "The path entered was: $password"
On linux and Windows terminal i get this:
>>bash try.sh
try.sh: line 2: $'\r': command not found
Type the username, followed by [ENTER]:
try.sh: line 4: $'\r': command not found
': not a valid identifierername
try.sh: line 6: $'\r': command not found
Type the password, followed by [ENTER]:
try.sh: line 8: $'\r': command not found
': not a valid identifierssword
try.sh: line 10: $'\r': command not found
The name entered was:
The path entered was:
I think your files have as line separators \r\n, which is vaid on windows but not on linux (at least for scripts intended to be read by bash). On linux the line separator should be \n

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

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

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