read answer #this will read what the user is typing in and makes a string for it
if [[ -z {answer} ]]
then
echo "Sorry that is not a valid choice, please select from the above list provided" #this will help pick up if the user just enters a blank option
if [[ $1{answer} ]]
then
echo "this has worked" #no clue if this will work going to try this and see if it works
if [[ $2{answer} ]]
then
echo "this is answer 2"
if [[ $3{answer} ]]
then
echo "this is answer 3"
if [[ $4{answer} ]]
then
echo "This is answer 4"
if [[ $Q{answer} ]]
then
echo "this is the quit button"
else
echo "no valid choice has been made"
fi
fi
fi
fi
fi
fi
so i am trying to run this code, i have closing arguments for each if statement, but every time i try and run it it shows "line 51: syntax error: unexpected end of file" the code goes up to line 50, i tried putting in ;; but it ran a syntax error on them, i tried converting with the dos2unix command but nothing, is there something i am missing?
Add the $ to this line, like so
if [[ -z ${answer} ]]
Related
I'm just learning terminal commands, I'm trying to create my own command, but I have the following problem
./myFile: line 10: syntax error in conditional expression
./myFile: line 11: syntax error near `then'
./myFile: line 11: ` then'
there is my code
#!/bin/bash
echo "please enter name of the folder"
read NAME1
if [[ $NAME1 ]]
then
echo "enter first number"
read NUM1
if [[ $NUM1 ]] && [[ $NUM1 -ge 0]]
then
echo "enter last number"
read NUM2
if [[ $NUM2 ]] && [[ $NUM2 -gt $NUM ]]
then
mkdir $NAME1{$NUM1..$NUM2}
else
echo"please enter last number to continue"
fi
else
echo "please enter first number to continue"
fi
else
echo "please enter name of the folder to continue"
fi
Firstly, the expression [[ $NAME1 ]] is not valid, or at least, does not do what you think it does. I believe you are trying to determine if the user actually typed in a string. To do this, you should either test for a non-empty string ([[ -n ${NAME1} ]]) or test for a string length greater than zero ([[ ${#NAME1} -gt 0 ]]). The same applies when you are testing $NUM1 and $NUM2.
Secondly, when dealing with user input, you should take care to avoid testing empty strings. This is best achieved by quoting your variables. For example: [[ "${NUM1}" -gt 0 ]].
Thirdly, spaces are important in tests. always leave a space after the [[ and before the ]].
In addition, $NUM (line 15) is not declared, so will evaluate to the empty string. This should be set somewhere earlier in the script.
There are many other areas in which this script could be improved (e.g. checking for numerical input, checking for valid folder names, etc. But the above changes should get you past the immediate errors.
Can someone explain why this simple bash script:
#!/bin/bash
myvar="Hello"
if [[ -z "$myvar" ]]; then
# echo "It's an unfilled string"
else
echo "It's a filled string!"
fi
gives me the error
./testscript: line 7: syntax error near unexpected token `else'
./testscript: line 7: `else'
However, if I remove the comment on the echo line, the script runs fine. Obviously, there is an issue with having commented lines within empty if statements. This this in mind, how do I fix it so I can have an empty if statement with comments?
There are no statements between then and else, so this is a syntax error. If you really want to do nothing in the if branch, then you can use a : (or true) as a placeholder:
#!/bin/bash
myvar="Hello"
if [[ -z "$myvar" ]]; then
# echo "It's an unfilled string"
:
else
echo "It's a filled string!"
fi
Better yet, reverse your logic:
#!/bin/bash
myvar="Hello"
if [[ -n "$myvar" ]]; then
echo "It's a filled string!"
fi
This is the way not to use if else statement.
#!/bin/bash
myvar="Hello"
[[ -n "$myvar" ]] && echo "It's a filled string!"
Also you can use this.
#!/bin/bash
myvar="Hello"
[[ -z "$myvar" ]] || echo "It's a filled string!"
I want an if/then statement in Bash and I can't seem to get it to work. I would like to say "If the line begins with > character, then do this, else do something else".
I have:
while IFS= read -r line
do
if [[$line == ">"*]]
then
echo $line'first'
else
echo $line'second'
fi
done
But it isn't working.
I also tried to escape the ">" by saying:
if [[$line == ^\>*]]
Which didn't work either.
Both ways I am getting this error:
line 27: [[>blah: command not found
Suggestions?
Spaces are needed inside [[ and ]] as follows:
if [[ "$line" == ">"* ]]; then
echo "found"
else
echo "not found"
fi
This attempt attempt uses a regex:
line="> line"
if [[ $line =~ ^\> ]] ; then
echo "found"
else
echo "not found"
fi
This one uses a glob pattern:
line="> line"
if [[ $line == \>* ]] ; then
echo "found"
else
echo "not found"
fi
Spacing is important.
$ [[ ">test" == ">"* ]]; echo $?
0
$ [[ "test" == ">"* ]]; echo $?
1
if grep -q '>' <<<$line; then
..
else
..
fi
using grep is much better :)
I need to check that the input consists only of numeric characters. I have the code below, but it didn't work properly.
if [[ $1 =~ [0-9] ]]; then
echo "Invalid input"
fi
It should give true only for 678686 not for yy66666.
How about this:-
re='^[0-9]+$'
if ! [[ $Number =~ $re ]] ; then
echo "error: Invalid input" >&2; exit 1
fi
or
case $Number in
''|*[!0-9]*) echo nonnumeric;;
*) echo numeric;;
esac
Try using start/end anchors with your pattern. If you don't, the match succeeds with a part of a test string. Don't forget that you have to use a pattern matching the complete test string if you follow this suggestion.
if [[ $1 =~ ^[0-9]+$ ]]; then
echo "Invalid input"
fi
Check out this SO post for more details.
I have a variable like below.
variable = This script is not found
if [[ "$variable" = ~ "not found" ]];
then
echo "Not Found"
else
echo "Its there"
if
while executing im getting below err,
line 4: syntax error in conditional expression
./test.sh: line 4: syntax error near `found"'
./test.sh: line 4: `if [[ "$variable" = ~ "not found" ]]; '
could anyone point me, What im missing here?
LIST="some string with a substring you want to match"
SOURCE="substring"
if echo "$LIST" | grep -q "$SOURCE"; then
echo "matched";
else
echo "no match";
fi
Good Luck ;)
Compare this with your version at the indicated points:
variable="This script is not found" # <--
if [[ "$variable" =~ "not found" ]] # <--
then
echo "Not Found"
else
echo "Its there"
fi # <--
You can't put spaces around = in an assignment, and you need to quote a string literal that has spaces. You don't need a trailing ; if you're going to put then on its own line. And an if-then ends with "fi" not "if".
here is a correct construction of your if statement
if [[ "$variable" =~ "not found" ]]; then
echo "Not Found";
else
echo "Its there";
fi
Input:
line="There\'s a substring to be found!"
if [[ "$line" =~ *"substring"* ]]; then
echo "Not Found";
else
echo "Found!"
fi
Output:
Found!
I have tried below codes which always return same result either in true or false
if [[ "$variable" =~ "not found" ]]; then
echo "Not Found";
else
echo "Its there";
fi