Bash if/else error [closed] - linux

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a script to compile a program in bash and my if/else statement on line 84 is not working. I get this error:
./build2.sh: 84: [: no: unexpected operator
What is the problem?
http://www.pasteall.org/62904/bash
Thanks in advance!

"Is not working" is very faint. However, I'll give it a try...
If enter is pressed without typing any reply, $putProgramOn is empty, and
if [ $putProgramOn == "no" ] resolves to if [ == "no" ], which is a syntax error, so you need at least to quote the reply in the conditional:
if [ "$putProgramOn" == "no" ]
See also Bash Comparison Operators.
BTW: With esac you can simply have shortcuts "y" and "n" evaluated as well:
read -p 'Would you like to put the program on the flash drive? Awnser yes or no: ' putProgramOn
case "$putProgramOn" in
n|no)
echo "putProgramOn=no"
;;
y|yes)
echo "putProgramOn=yes"
;;
*)
echo "putProgramOn=\"putProgramOn\""
exit 1
;;
esac

Related

simplest bash string comparison in makefile is failing [duplicate]

This question already has answers here:
Why am i getting an unexpected operator error in bash string equality test? [duplicate]
(2 answers)
Bash scripting unexpected operator
(1 answer)
Closed 3 years ago.
My makefile is as follows:
test:
if [ "a" == "a" ]; then echo "hooray"; fi
running make test yields the following error:
/bin/sh: 1: [: a: unexpected operator
(Note, if I change the conditional to "a" == "b", the error still refers to "a", so the "problem" is (at least) with the first "a").
I feel like I must be missing something silly, but I cannot get this to work, and can't think of any way to further simplify the problem.
I'm on a raspberry pi (raspbian), running the default/latest everything (apt-get update && apt-get upgrade daily).
Gah. The clue is /bin/sh. apparently make doesn't use bash as its shell under my configuration? very strange...
Anyways, == isn't POSIX sh. Even this cheat sheet: https://www.joedog.org/articles-cheat-sheet/ gets it wrong (thank you Rebecca Cran, who commented as such a year ago...)
To be clear- the solution is to use a single =, as in:
test:
if [ "a" = "a" ]; then echo "hooray"; fi

Getting error with if condition [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What is wrong with my below script?
I am new to scripting and trying out simple scripts. I am getting an error with my script.
START_OUT = `grep -c "Start Report" jeevagan/test_scripts/log.txt`
FINISH_OUT = `grep -c "Finished Report" jeevagan/test_scripts/log.txt`
if [$START_OUT == $FINISH_OUT]
then
echo "All good"
else
echo "Warning!!!Monitor Logs"
fi
bash/sh is very space sensitive. You want
if [ $START_OUT == $FINISH_OUT ]
Note the spacing around the brackets. "=" may be used instead of "==" for strict POSIX compliance. See here for more details and note the comment
In a script, the different parts of the if statement are usually
well-separated.
try leaving spaces on the if statement and add quotes on the variables, like this
if [ "$START_OUT" == "$FINISH_OUT" ]
If this is still not working try this exactly, this works for sure, if it still gives you an error, then use echo brefore the if statement and check what $START_OUT and $FINISH_OUT variables have stored, because the problem could be there, after you do this give us feedback:
if test "$START_OUT" = "$FINISH_OUT"
okayyyy, this was one of your problems, the other problem is that you left a space when you used grep and tried to add it in the variable...
i will rewrite your code, copy and paste it please. as you had it, it was like you calling the START_OUT command which obviously doesnt exist.... and tell me if it worked.
START_OUT=`grep -c "Start Report" jeevagan/test_scripts/log.txt`
FINISH_OUT=`grep -c "Finished Report" jeevagan/test_scripts/log.txt`
if test "$START_OUT" = "$FINISH_OUT"
then
echo "All good"
else
echo "Warning!!!Monitor Logs"
fi
if [$START_OUT == $FINISH_OUT] is wrong. take care about spaces. it should be like
if [ $START_OUT == $FINISH_OUT ]

syntax error near unexpected token `else' [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to Linux and Shell scripting, so please help me with some patience.
Could you please let me know how to fix this problem?
Below is the error
./test.sh: line 5: syntax error near unexpected token `else'
./test.sh: line 5: `else'
Below is the simple if statment in shell scripting.
I use vi editor
#!/bin/bash -x
age=10;
if(age -lt 13)
echo "$age"
else
echo "xx"
Any information is highly appreciable.
Matt
Don't end a statement with ;. This is not c/c++/java program.
Conditions in script are enclosed with [] not ()
Please read how to use if statement/syntax. Do google and then try.
Syntax of if:
if [ conditional expression ]
then
#statements
...
fi
So your program is:
#!/bin/bash
age=10
if [ $age -lt 13 ]
then
echo "something"
else
echo "some other thing"
fi

Bash file existence checking missing ] [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am attempting a simple file existence check in my bash script:
FILE_TO_CHECK=/home/username/path/to/file
if [ ! -f $FILE_TO_CHECK]; #line 9 in actual script
then
echo File not found.
fi
Seems simple enough to me, but I'm getting the following error and I don't know why:
/path/to/script: line 9: [: missing `]'
I'm not exactly a bash expert, but I was pretty sure a backtick is not necessary in this context. What gives?
Missing space before the closing ].
You have to understand that [ is a command and everything following it, until the ;, are its arguments. Command [ expects its last argument to be ]. But if you omit the space, then ] becomes the last character of the previous argument.
It might seem that [ ] is part of the if syntax. It's not the case. if has to be followed by any command, and if evaluates its exit status.
if true; then foo; fi
In the above line true is a command too.
$ which true
/bin/true
true is a command with the sole purpose of always having a true (0) exit status.
You could also try:
if (test ! -f $FILE_TO_CHECK);
or
if !(test -f $FILE_TO_CHECK);
as [ is a shorthand for the test command.

How to view process name in terminal-emulator tab or title bar [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How can I put the current running process name into a GNOME Terminal tab title (or title bar when there's only one tab)?
Although https://superuser.com/questions/42362/gnome-terminal-process-name-in-tab-title provides a solution (below), it completely clutters each tab with garbage when it starts so as to appear broken. Is there a better way?
case "$TERM" in
xterm*|rxvt*)
set -o functrace
trap 'echo -ne "\e]0;$BASH_COMMAND\007"' DEBUG
PS1="\e]0;\s\007$PS1"
;;
*)
;;
esac
Well, since everyone already seems to know David Pashley's solution I'm kind of surprised it took me so long to find this one.
It actually takes care of the bash-completion problem.
To be clear: I did nothing on my own here but research. All credit goes to Marius Gedminas (http://mg.pov.lt/blog/bash-prompt.html).
This works perfectly for me with Gnome-Terminal/Terminator
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}#${HOSTNAME}: ${PWD}\007"'
# Show the currently running command in the terminal title:
# http://www.davidpashley.com/articles/xterm-titles-with-bash.html
show_command_in_title_bar()
{
case "$BASH_COMMAND" in
*\033]0*)
# The command is trying to set the title bar as well;
# this is most likely the execution of $PROMPT_COMMAND.
# In any case nested escapes confuse the terminal, so don't
# output them.
;;
*)
echo -ne "\033]0;${USER}#${HOSTNAME}: ${BASH_COMMAND}\007"
;;
esac
}
trap show_command_in_title_bar DEBUG
;;
*)
;;
esac

Resources