Fedora assign date to a variable to get an integer - shell

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"

Related

Bash script error / integer expression expected [duplicate]

To satisfy function requirements I have to retrieve a parameter which is a duty cycle (0-100% in 0.01%).
For test, I wrote something simple like :
#!/bin/bash
#
if [ "$1" -lt 0 ] || [ "$1" -gt 100 ]
then
echo "bad param"
else
echo "ok"
fi
I obtain :
root#:~# ./test.sh 11
ok
root#:~# ./test.sh 11,01
./test.sh: line 4: [: 11,01: integer expression expected
./test.sh: line 4: [: 11,01: integer expression expected
bad param
How to realise this kind of test ?
bash can only operate in integer arithmetics. If you need floats, use an external tool like bc:
if (( $( bc <<< "($1 < 0) || ($1 > 100) " ) )) ; then
...
fi
I would rather switch to a more powerful language for the whole script, though (like Perl)/

bash shell script, issues with a 32 bit decimal to binary converter

Having an issue with this
when I run it, the console repeats this over and over;
14: shellscript.sh: 2: not found
15: shellscript.sh: 32: not found
18: shellscript.sh: =1: not found
19: shellscript.sh: 0: not found
It seems like it's something to do with how bash handles redefining variables through arithmetic?
#!/bin/bash
echo "This script converts a user's number into an IP address."
echo -n "Input your number: "
read user
if [ $user -lt 4294967296 ]
then
exp=$((32))
num=$((0))
ipb=""
while [ $exp -gt 0 ]
do
bit=expr 2 ** $exp
exp=expr $exp - 1
if [ $bit+$num -le $user ]
then
$ipb="${ipb}1"
num=expr $num + $bit
else
$ipb="${ipb}0"
fi
done
echo $ipb
echo "done"
fi
Same as above but with comments to explain it.
#!/bin/bash
echo "This script converts a user's number into an IP address."
echo -n "Input your number: "
read user
#check if number is larger than 32bits
if [ $user < 4294967296 ]
then
#var exp is exponent that will be used to redefine var bit each loop cycle
#var num is var used to rebuild the user number with corresponding bits added to -
#var ipb is IP binary (not yet converted to 4 octet integers)
exp=$((32))
num=$((0))
ipb=""
#while the exponent is greater than 0 (exponent is 1 less as per binary order)
while [ $exp > 0 ]
do
#(Re)define bit var for line 23
bit=expr 2**$exp
#go to next lowest exponent
exp=expr $exp - 1
#If the current bit is added to our num var,will it be
#less than or equal to the user number?
if [ $bit + $num -le $user ]
then
#If so, redefine the ipb string var with a 1 on the end
#and redefine the num integer var added with the current
#iteration of the bit integer var's value
$ipb="${ipb}1"
num=expr $num + $bit
else
#if not, redefine the ipb string var with a 0 on the end
$ipb="${ipb}0"
fi
done
#output the IP binary
echo $ipb
echo "done"
fi
EDIT:
After some googling and help from shellcheck I got it to work. for some reason with my version of linux mint, the let command was the only thing that correctly took 2**31 as an exponent operation. Here's the code for anyone curious.
echo "This script converts a user's number into the 32 bit equivalent."
echo -n "Input a number below 4294967296: "
read user
echo ""
if [ $user -lt 4294967296 ]
then
exp=$((31))
num=$((0))
ipb=""
while [ $exp -ge 0 ]
do
let bit=2**$exp
let exp-=1
if (( $bit + $num <= $user ))
then
ipb="${ipb}1"
num=$(($num + $bit))
else
ipb="${ipb}0"
fi
done
fi
echo $ipb
Be sure to use bash instead of sh or ./ when running the script in terminal.
Your script has several issues. This one does not throw any errors.
#! /bin/bash
echo "This script converts a user's number into an IP address."
echo -n "Input your number: "
read user
if [ $user -lt 4294967296 ]
then
exp=$((32))
num=$((0))
ipb=""
while [ $exp -gt 0 ]
do
bit=$((2 ** $exp))
exp=$(expr $exp - 1)
if (( bit+num < user ))
then
ipb="${ipb}1"
num=$(expr $num + $bit)
else
ipb="${ipb}0"
fi
done
echo $ipb
echo "done"
fi
One major issue is that expr cannot calculate powers. You also do not use command substitution when you are trying to assign the result of expr to a variable.
Another major issue is that you cannot do math inside single [ ] brackets as you try to do in an if branch. Use (( )) instead.
There were a few other issue, e.g., $ipb="${ipb}1" expanding the variable that you actually want to assign.

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.

not found error in shell script

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.

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

Resources