not found error in shell script - shell

I am trying to run the following code in debian terminal.
read var
if [$var -gt 0]; then
echo "Greater than zero"
fi
When I give 45 as an input to the variable var, the terminal is showing the following error
4:[ 45: Not Found
Why is the error coming and what is it's solution.

Spaces inside [ and ] are mandatory:
Try:
read var
if [ "$var" -gt 0 ]; then
echo "Greater than zero"
fi
/bin/[ is a binary in Unix that takes it's arguments from the string that comes after a space.

Related

convert read string to integer in bash

I'm trying to read from user and then do the following.
read framechoice
if [ $framechoice -gt 100 ]; then
if [ $framechoice -lt 0 ]; then
framechoice=101
fi
fi
It gives me the following error.
[: -gt: unary operator expected
Can anyone tell me where I am going wrong.
This happens if you don't input anything:
$ cat myscript
read framechoice
if [ $framechoice -gt 100 ]; then
if [ $framechoice -lt 0 ]; then
framechoice=101
fi
fi
$ bash myscript
<enter>
myscript: line 2: [: -gt: unary operator expected
Try instead to actually enter something:
$ bash myscript
42<enter>
The script then exits with success.
Your program needs to cope with empty input. This is most easily achieved by properly quoting the variable; then
if [ "$framechoice" -gt 100 ]; then
evaluates to [ "" -gt 100 ] which is no longer is a syntax error; however, instead, it throws the warning integer expression expected.
Even better, maybe filter the input so that you do not attempt numeric comparisons before making sure the input is numeric.

Fedora assign date to a variable to get an integer

I'm new in linux environment and shell programming. Im trying to do a simple shell scripting program where I want to display "Good Morning" Good Afternoon" Good Evening" or "Good Night" according to the time in the system. I want to do this using date function as well.
Below is the code I've typed to do the above task.
#!bin/bash
$ say=$(date +%H)
echo "echoo $say"
if [ "$say" -gt 18 ]
then
var="Night"
elif [ "$say" -gt 15 ]
then
var="Evening"
elif [ "$say" -gt 12 ]
then
var="Afternoon"
else
var="Morning"
fi
echo "hello $USER , $var"
What I planned to do here is get the hours part to a variable and check in which range that integer falls in to give the variable morning,night or whatever the value matches.
Below is the output I get when I execute the shell script
time.sh: line 3: $: command not found
echoo
time.sh: line 8: [: : integer expression expected
time.sh: line 12: [: : integer expression expected
time.sh: line 16: [: : integer expression expected
hello achala , Morning
Please help me through this. Thanx in advance.
The first line should be: say=$(date "+%H")
Try this:
#!bin/bash
say=$(date "+%H")
echo "echoo $say"
if [ "$say" -gt 18 ]
then
var="Night"
elif [ "$say" -gt 15 ]
then
var="Evening"
elif [ "$say" -gt 12 ]
then
var="Afternoon"
else
var="Morning"
fi
echo "hello $USER , $var"

Mac Bash - doesn't recognize if[$#=0]

It's been a while since I've scripted in bash so I made a small script to test things out. This is my script (the quoted text is some Dutch, doesnt really matter):
#isingelogd
if[$#=0]
then
echo "Geef user-id's op!" 1>$2 ; exit 1
fi
for uid in $*
do
if who|grep $uid >dev/null
then
echo $uid is ingelogd
else
echo $uid is niet ingelogd
fi
done
If I try to run it it tells me the following:
bash-3.2$ ./isingelogd
./isingelogd
./isingelogd: line 3: if[0=0]: command not found
./isingelogd: line 4: syntax error near unexpected token `then'
./isingelogd: line 4: `then'
If I check my version with bash -v I'm running 3.2 which I thought supported square brackets.
Has someone had a similar problem and found solution?
Look at your errors:
bash-3.2$ ./isingelogd
./isingelogd
./isingelogd: line 3: if[0=0]: command not found
./isingelogd: line 4: syntax error near unexpected token then'
./isingelogd: line 4:then'
See that command not found? you have an error in your script.
The [..] are actual commands, and like all commands, they need to be separated by white spaces. The = is a parameter to the [ command and also needs to be surrounded by white space. Change line #3 to this:
if [ $# -eq 0 ]
Since $# and 0 are numeric, you should use -eq which compares to numbers and not = which compares strings.
Try these commands:
$ ls -li /bin/test
$ ls -li /bin/[
You'll see they have the same inode number. They're links. (Yes, the [ and test are builtins into the shell, but they are linked builtin commands).
$ man test
will give you all of the various tests that [ can do. Again, note the difference between -eq vs. = and -gt vs. >.
Note the following:
if [ 54 > 123 ]
then
echo "54 is greater than 123"
fi
This will print out "54 is greater than 123". This won't:
if [ 54 -gt 123 ]
then
echo "54 is greater than 123"
fi
a.bash works for me in Mac. Content of a.bash is the following :
#!/bin/bash
if [ $# == 0 ]; then
echo "Usage da da do"
fi
export A=$1
echo $A
then execute with the following :
\# ] ./a.bash
Usage da da do

Bash script command result inside other variable to define prompt

I would like to define a prompt which will indicate with colors whether the command executed properly and whether the command was found. As for now I have something like this but I does not work properly.
PS1="\`COMMAND_RESULT=\$\?;
if [ $COMMAND_RESULT -eq 127 ]; then echo \[\e[33m\] ---=== Command not found ===--- ;
elif [ $COMMAND_RESULT -ne 0 ]; then echo \[\e[33m\]---=== \[\e[31m\]Oh noes, bad command \[\e[33m\]==---;
fi\`
\n\[\e[0;37m\][\[\e[1;31m\]\#\[\e[0;37m\]]
\[\e[0;32m\]\u\[\033[1;33m\]#\[\033[0;32m\]\h
As for now I get this error on bash start :
-bash: [: -eq: unary operator expected
-bash: [: -ne: unary operator expected
Don't pollute your PS1 with functions. You should use the special PROMPT_COMMAND variable to do this. The value of PROMPT_COMMAND is executed as a command prior to issuing each primary prompt.
Here is an example:
_check_command(){
local COMMAND_RESULT=$?
if [ $COMMAND_RESULT -eq 127 ]
then
echo -e "\e[1;33m---=== Command not found ===---\e[m"
elif [ $COMMAND_RESULT -ne 0 ]
then
echo -e "\e[1;31m---=== Oh noes, bad command ===---\e[m"
fi
}
PROMPT_COMMAND='_check_command'
PS1="\[\e[0;37m\][\[\e[1;31m\]\#\[\e[0;37m\]] \[\e[0;32m\]\u\[\033[1;33m\]#\[\033[0;32m\]\h "
There are many bash prompts you can find online to guide you. Here is one good example.
You probably should not escape $? as \$\?. Looks like it gets interpreted literally.
Also you can check out the Arch Wiki article that shows how to implement something similar to what you want. Look at this line:
PS1="$(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]\h'; else echo '\[\033[01;32m\]\u#\h'; fi)\[\033[01;34m\] \w \$([[ \$? != 0 ]] && echo \"\[\033[01;31m\]:(\[\033[01;34m\] \")\$\[\033[00m\] "
especially this part:
([[ \$? != 0 ]] && echo \"\[\033[01;31m\]:(\[\033[01;34m\] \")

Bash script string comparison error, can't figure out

I fixed the code below so it works:
#!/bin/bash
out="$(cat /proc/acpi/bbswitch)"
if [[ "$out" == *OFF* ]];
then
tee /proc/acpi/bbswitch <<<ON
echo "Nvidia card activated."
else
tee /proc/acpi/bbswitch <<<OFF
echo "Nvidia card disabled."
fi
This is made for activating or disabling my optimus card. I get an error on line 4:
./.bb: line 4: [0000:01:00.0 OFF: command not found
OFF
Nvidia card disabled.
I can read from it that it tries to execute the $out variable. Why?
You need to ensure that there is at least 1 space between the brackets [ / ] and the actual variables; i.e.: change your code from
if ["$out" == "$is"];
to:
if [ "$out" == "$is" ];
And it should work.
The reason is that [ is actually the "test" command in bash. Try on your prompt:
which [
and you should see something like:
/usr/bin/[
Also, man [ to read more about syntax
(Note, since arguments are delimited by spaces, there needs to be a space between your 2nd variable and ] as well. Test uses ] as the terminating sentinel)

Resources