bash while loop not exiting and not echoing - linux

So I have this bash script
function exec_do(){
while [[ 1 ]]; do
read _INPUT
if [$_INPUT -eq "exit"]
then
break
else
echo $_INPUT
fi
done
}
The intention is so that if I type in exec_do then it does a while loop which reads the input and do stuff based on the input.
If the input is exit, then it exits the while loop
If the input is not exit then it echoes it
However when I run exec_do
and then type in input
It instead returns input: command not found
And moreover typing in "exit" doesn't break the loop and also return command not found
What did I do wrong and how can I fix this?

Your comparison for _$INPUT is a string, then you need ==. If comparing integers, you need -eq. Also double quote the $_INPUT variable for strings if they might contain white spaces or meta characters.
#!/bin/bash
function exec_do(){
while [[ 1 ]]; do
read _INPUT
if[ "$_INPUT" == "exit" ]; then
break
else
echo $_INPUT
fi
done
}
exec_do

Related

Input through pipe in separate terminal

Hi I am trying to build a program in bash. The idea is that I would have open two terminals. One would take the input with a pipe cat > pipe . The other terminal would be running a bash script with a while true loop and read the input from the pipe. The input would be stored to a variable and further action would occur depending on what is stored inside. This is what I tried.
The program gets the pipe name as an argument and it is stored to the variable pipe.
while true; do
input=$(cat<$pipe)
if [ "$input" == "exit" ]; then
exit 0
fi
done
I have tried to input an exit string throught the pipe but the program does not stop as it should. If the variable does not get any value from the pipe how would I correct that? Or is something else wrong that prevents the exit from happening?
Your second script should be something like :
#!/bin/bash
pipe="$1" # Here $1 is full path to the file pipe as you have confirmed
while true
do
input=$(cat<"$pipe")
if [[ $input =~ exit ]] #original line was if [ "$input" == "exit" ]
then
exit 0
fi
done
Remember $(cat<"$pipe") will store the entire file as a string. So, even though you have the word exit somewhere down in the pipe file the original condition if [ "$input" == "exit" ] will be false except the case when the first word you enter for cat>pipe itself is "exit".
A little tweakish solution is do a regex match (=~) as I have done but this solution is not very reliable because if you enter something like "I don't wanna exit", for cat>pipe the second script will exit.
A second solution would be :
#!/bin/bash
pipe="$1"
while true; do
input=$(tail -n1 "$pipe") #checking only the last line
if [[ "$input" == "exit" ]] #original if condition
then
exit 0
fi
done

How do I deal with empty user input in a Bash script?

When the script asks me for input, I get an error if I just press Return without typing in anything. How do I fix this?
Here's the script:
#!/bin/bash
SUM=0
NUM=0
while true
do echo -n "Pruefungspunkte eingeben ('q' zum Beenden): "
read SCORE
if test "$SCORE" == "q"
then echo "Durchschnittspunktzahl: $AVERAGE."
break
else SUM=`expr $SUM + $SCORE`
NUM=`expr $NUM + 1`
AVERAGE=`expr $SUM / $NUM`
fi
done
How about using good bash practices?
#!/bin/bash
sum=0
num=0
while true; do
read -erp "Pruefungspunkte eingeben ('q' zum Beenden): " score
if [[ $score = q ]]; then
echo "Durchschnittspunktzahl: $average."
break
elif [[ $score =~ ^-?[[:digit:]]+$ ]]; then
((sum+=10#$score))
((++num))
((average=sum/num))
else
echo "Bad number"
fi
done
Good practice:
don't use capitalized variable names
use the [[ builtin instead of the test builtin
don't use backticks, use (( to invoke shell arithmetic
to make sure the user inputs a number, check that a number was really entered. The line
elif [[ $score =~ ^-?[[:digit:]]+$ ]]; then
just does that (see regular expressions). Incidentally it completely solves your original problem, since an empty input will not pass through this test
to prevent problems if a user enters 09 instead of 9, force bash to interpret the input in radix 10. That's why I'm using (10#$score) instead of just score.
Use read with the -p (prompt) option, instead of the clumsy combo echo -n / read
This version is much more robust and well-written than yours. Yet, it still has problems:
will break if user needs large numbers
as shell arithmetic is used, only integers can be used. Moreover, the average given by this program is rounded: if you want the average of 1 and 2 you'll have 1.
To fix both problems, you'll probably want to use bc or dc. But that will be the purpose of another question. Or not.
Initialise $SCORE beforehand or handle empty input like you do in q case.
[[ -z "$SCORE" ]] && echo "\$SCORE is zero, e.g. \"\""
This will test if the variable SCORE is empty string.
You should also set AVERAGE=0 at the beginning.

Bash reading txt file and storing in array

I'm writing my first Bash script, I have some experience with C and C# so I think the logic of the program is correct, it's just the syntax is so complicated because apparently there are many different ways to write the same thing!
Here is the script, it simply checks if the argument (string) is contained in a certain file. If so it stores each line of the file in an array and writes an item of the array in a file. I'm sure there must be easier ways to achieve that but I want to do some practice with bash loops
#!/bin/bash
NOME=$1
c=0
#IF NAME IS FOUND IN THE PHONEBOOK THEN STORE EACH LINE OF THE FILE INTO ARRAY
#ONCE THE ARRAY IS DONE GET THE INDEX OF MATCHING NAME AND RETURN ARRAY[INDEX+1]
if grep "$NOME" /root/phonebook.txt ; then
echo "CREATING ARRAY"
while read line
do
myArray[$c]=$line # store line
c=$(expr $c + 1) # increase counter by 1
done < /root/phonebook.txt
else
echo "Name not found"
fi
c=0
for i in myArray;
do
if myArray[$i]="$NOME" ; then
echo ${myArray[i+1]} >> /root/numbertocall.txt
fi
done
This code returns the only the second item of myArray (myArray[2]) or the second line of the file, why?
The first part (where you build the array) looks ok, but the second part has a couple of serious errors:
for i in myArray; -- this executes the loop once, with $i set to "myArray". In this case, you want $i to iterate over the indexes of myArray, so you need to use
for i in "${!myArray[#]}"
or
for ((i=0; i<${#a[#]}; i++))
(although I generally prefer the first, since it'll work with noncontiguous and associative arrays).
Also, you don't need the ; unless do is on the same line (in shell, ; is mostly equivalent to a line break so having a semicolon at the end of a line is redundant).
if myArray[$i]="$NOME" ; then -- the if statement takes a command, and will therefore treat myArray[$i]="$NOME" as an assignment command, which is not at all what you wanted. In order to compare strings, you could use the test command or its synonym [
if [ "${myArray[i]}" = "$NOME" ]; then
or a bash conditional expression
if [[ "${myArray[i]}" = "$NOME" ]]; then
The two are very similar, but the conditional expression has much cleaner syntax (e.g. in a test command, > redirects output, while \> is a string comparison; in [[ ]] a plain > is a comparison).
In either case, you need to use an appropriate $ expression for myArray, or it'll be interpreted as a literal. On the other hand, you don't need a $ before the i in "${myArray[i]}" because it's in a numeric expression context and therefore will be expanded automatically.
Finally, note that the spaces between elements are absolutely required -- in shell, spaces are very important delimiters, not just there for readability like they usually are in c.
1.-This is what you wrote with small adjustments
#!/bin/bash
NOME=$1
#IF NAME IS FOUND IN THE PHONE-BOOK **THEN** READ THE PHONE BOOK LINES INTO AN ARRAY VARIABLE
#ONCE THE ARRAY IS COMPLETED, GET THE INDEX OF MATCHING LINE AND RETURN ARRAY[INDEX+1]
c=0
if grep "$NOME" /root/phonebook.txt ; then
echo "CREATING ARRAY...."
IFS= while read -r line #IFS= in case you want to preserve leading and trailing spaces
do
myArray[c]=$line # put line in the array
c=$((c+1)) # increase counter by 1
done < /root/phonebook.txt
for i in ${!myArray[#]}; do
if myArray[i]="$NOME" ; then
echo ${myArray[i+1]} >> /root/numbertocall.txt
fi
done
else
echo "Name not found"
fi
2.-But you can also read the array and stop looping like this:
#!/bin/bash
NOME=$1
c=0
if grep "$NOME" /root/phonebook.txt ; then
echo "CREATING ARRAY...."
readarray myArray < /root/phonebook.txt
for i in ${!myArray[#]}; do
if myArray[i]="$NOME" ; then
echo ${myArray[i+1]} >> /root/numbertocall.txt
break # stop looping
fi
done
else
echo "Name not found"
fi
exit 0
3.- The following improves things. Supposing a)$NAME matches the whole line that contains it and b)there's always one line after a $NOME found, this will work; if not (if $NOME can be the last line in the phone-book), then you need to do small adjustments.
!/bin/bash
PHONEBOOK="/root/phonebook.txt"
NUMBERTOCALL="/root/numbertocall.txt"
NOME="$1"
myline=""
myline=$(grep -A1 "$NOME" "$PHONEBOOK" | sed '1d')
if [ -z "$myline" ]; then
echo "Name not found :-("
else
echo -n "$NOME FOUND.... "
echo "$myline" >> "$NUMBERTOCALL"
echo " .... AND SAVED! :-)"
fi
exit 0

While loop in Bash script?

I'm not used to writing Bash scripts, and Google didn't help in figuring out what is wrong with this script:
#!/bin/bash
while read myline
do
done
echo "Hello"
while read line
do
done
exit 0
The output I get is:
./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'
and my bash version is:
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Thank you.
Edit: The script works OK when the while loop isn't empty.
While I'm at it... I expected to exit the loop when the user typed nothing, ie. simply hit the Enter key, but Bash keeps looping. How can I exit the loop?
while read myline
do
echo ${myline}
done
echo "Hello"
while read line
do
true
done
exit 0
You can't have an empty loop. If you want a placeholder, just use true.
#!/bin/bash
while read myline
do
true
done
or, more likely, do something useful with the input:
#!/bin/bash
while read myline
do
echo "You entered [$line]"
done
As for your second question, on how to exit the loop when the user just presses ENTER with nothing else, you can do something:
#!/bin/bash
read line
while [[ "$line" != "" ]] ; do
echo "You entered [$line]"
read line
done
When you are using the read command in a while loop it need input:
echo "Hello" | while read line ; do echo $line ; done
or using several lines:
echo "Hello" | while read line
do
echo $line
done
This is a way to emulate a do while loop in Bash, It always executes once and does the test at the end.
while
read -r line
[[ $line ]]
do
:
done
When an empty line is entered, the loop exits. The variable will be empty at that point, but you could set another variable to its value to preserve it, if needed.
while
save=$line
read -r line
[[ $line ]]
do
:
done
What are you trying to do? From the look of it it seems that you are trying to read into a variable?
This is done by simply stating read the value can then be found inside of $
ex:
read myvar
echo $myvar
As other have stated the trouble with the loop is that it is empty which is not allowed.
If you are looking to create a menu with choices as a infinite loop (with the choice to break out)
PS3='Please enter your choice: '
options=("hello" "date" "quit")
select opt in "${options[#]}"
do
case $opt in
"hello") echo "world";;
"date") echo $(date);;
"quit")
break;;
*) echo "invalid option";;
esac
done
PS3 is described here
If you type help while in bash you'll get an explanation of the command.

How to detect spaces in shell script variable [duplicate]

This question already has answers here:
How to check if a string has spaces in Bash shell
(10 answers)
Closed 3 years ago.
e.g string = "test test test"
I want after finding any occurance of space in string, it should echo error and exit else process.
The case statement is useful in these kind of cases:
case "$string" in
*[[:space:]]*)
echo "argument contains a space" >&2
exit 1
;;
esac
Handles leading/trailing spaces.
There is more than one way to do that; using parameter expansion
you could write something like:
if [ "$string" != "${string% *}" ]; then
echo "$string contains one or more spaces";
fi
For a purely Bash solution:
function assertNoSpaces {
if [[ "$1" != "${1/ /}" ]]
then
echo "YOUR ERROR MESSAGE" >&2
exit 1
fi
}
string1="askdjhaaskldjasd"
string2="asjkld askldja skd"
assertNoSpaces "$string1"
assertNoSpaces "$string2" # will trigger error
"${1/ /}" removes any spaces in the input string, and when compared to the original string should be exactly the same if there are not spaces.
Note the quotes around "${1/ /}" - This ensures that leading/trailing spaces are taken into consideration.
To match more than one character, you can use regular expressions to define a pattern to match - "${1/[ \\.]/}".
update
A better approach would be to use in-process expression matching. It will probably be a wee bit faster as no string manipulation is done.
function assertNoSpaces {
if [[ "$1" =~ '[\. ]' ]]
then
echo "YOUR ERROR MESSAGE" >&2
exit 1
fi
}
For more details on the =~ operator, see the this page and this chapter in the Advanced Bash Scripting guide.
The operator was introduced in Bash version 3 so watch out if you're using an older version of Bash.
update 2
Regarding question in comments:
how to handle the code if user enter
like "asd\" means in double quotes
...can we handle it??
The function given above should work with any string so it would be down to how you get input from your user.
Assuming you're using the read command to get user input, one thing you need to watch out for is that by default backslash is treated as an escape character so it will not behave as you might expect. e.g.
read str # user enters "abc\"
echo $str # prints out "abc", not "abc\"
assertNoSpaces "$str" # no error since backslash not in variable
To counter this, use the -r option to treat backslash as a standard character. See read MAN Page for details.
read -r str # user enters "abc\"
echo $str # prints out "abc\"
assertNoSpaces "$str" # triggers error
The == operator inside double brackets can match wildcards.
if [[ $string == *' '* ]]
You can use grep as:
string="test test test"
if ( echo "$string" | grep -q ' ' ); then
echo 'var has space'
exit 1
fi
I just ran into a very similar problem while handling paths. I chose to rely on my shell's parameter expansion rather than looking for a space specifically. It does not detect spaces at the front or the end, though.
function space_exit {
if [ $# -gt 1 ]
then
echo "I cannot handle spaces." 2>&1
exit 1
fi
}

Resources