how to solve nested if issue - unix

I am new to UNIX ... I am trying to write a bash script that takes two integers from the user and prints out the even numbers between these two numbers using an if condition. I am stuck on the nested if "an unexpected token near else" error message appears. I do not know what the error is about. Any help?
This is what I have done so far:
echo plz enter first number
read n1
echo plz enter second number
read n2
start=$n1
end=$n2
if [ start < end ] then
for (c=start;c<=end;c++)
do
if [ $((c % 2 )) -eq 0 ]; then
echo $c
fi
done
else
echo "not bigger"
fi

I think I would recommend a different approach:
((start % 2)) && ((start = 1 + start))
while ((start < end))
do
echo ${start}
(( start += 2))
done

I have tried like this:-
echo "Enter first number"
read first
echo "Enter second number"
read second
start=$first
endLine=$second
while [ $start -le $endLine ]
do
if [ $((start % 2 )) -eq 0 ]
then
echo $start "is an even number"
#else
# echo $start "is an odd number"
fi
start=`expr $start + 1`
done

You need to insert either a semicolon or a newline before the first "then":
if [ start < end ] ; then
^

Related

how to compare real/float while using "AND" .(unix)

I'm new to shell scripting, I have written a small script to check if the input is in the specified (integer) range:
echo "Enter number btw 50 and 100:"
read num
if [ $num -le 100 -a $num -ge 50 ];
then
echo "you are in limits."
else
echo "you are out of limits."
fi
How can I check if a number is in a real/floating range?
You can't with sh/ksh1/bash(/zsh?) -- integer arithmetic only.
You can do something like
if [ "$(echo "50 <= $num && $num <= 100" | bc)" = "1" ]; then
echo "you are in limits."
else
echo "you are out of limits."
fi
(1 - ksh93 can do floating point arithmetic)

Unix- How to return +,-,0 depending on integer inputted?

I would like to create shell script that echos a + if the inputted integer is a positive, echos a "-" if the inputted integer is a negative, and echos a "0" if its not any of those.
So my script is like this:
echo "Enter an Integer"
read declare -i var1
if((var1>0))
then
echo +
elif (( var1<0))
then echo -
else echo 0
fi
I keep getting a bad variable name error and var1 not found.
I got the script to work by separating the read and declare lines.
echo "Enter an Integer"
declare -i var1
read var1
if((var1>0))
then
echo +
elif (( var1<0))
then echo -
else echo 0
fi
Console log (running on OS X):
$ bash e.sh
Enter an Integer
972
+
$ bash e.sh
Enter an Integer
-32
-
$ bash e.sh
Enter an Integer
0
0
I believe you want to read and then test your values. Something like
echo "Enter an Integer"
read var1
if [[ "$var1" -gt 0 ]]
then
echo +
elif [[ "$var1" -lt 0 ]]
then
echo -
else
echo 0
fi
works here.

While loop with a certain amount of loops

repeat=0
echo "Please enter a number dividable by 5"
read input
loop= `expr $input / 5`
while [ $repeat -ne $loop ]
do
echo "repeat"
repeat= `expr $repeat + 1`
done
For this code, so if i enter 15, the code will repeat 3 times, it will echo 3 times of repeat, but from now i keep getting command not find from "repeat =expr $repeat + 1. not sure what is happening.
Spacing is important in bash. You need to remove the space after the =. i.e.
loop=`expr $input / 5`
and
repeat=`expr $repeat = 1`
With that space in place, it is trying to execute the result of the expr call, and complaining because there is no command called 3
You can't have a space after the = sign in setting the loop and repeat variable

Unix for loop, prompt for value. Read value into ArrayVar[i]

I am trying to do a nested if statement inside of a while loop. I get a
unexpected "end of file".
while :
do
if [ "$CHOICE" != "x" -o "$CHOICE != "X" ]
then
echo "Enter two whole numbers seperated by a space ex:1 123"
read Num1 Num2
if echo "$Num1$Num2" | egrep '^[0-9]+$' 2>/dev/null
then
# Num1 and Num 2 are INTS
break
else
break
fi
else
# One of the numbers is not an INT
printf 'Error: You did not enter two whole numbers, Try Again.\n\n'
continue
fi
done
echo "$Num1 $Num2"
if [ "$CHOICE" != "x" -o "$CHOICE != "X" ]
^
/
missing closing quote ---

Convert decimal to hexadecimal in UNIX shell script

In a UNIX shell script, what can I use to convert decimal numbers into hexadecimal? I thought od would do the trick, but it's not realizing I'm feeding it ASCII representations of numbers.
printf? Gross! Using it for now, but what else is available?
Tried printf(1)?
printf "%x\n" 34
22
There are probably ways of doing that with builtin functions in all shells but it would be less portable. I've not checked the POSIX sh specs to see whether it has such capabilities.
echo "obase=16; 34" | bc
If you want to filter a whole file of integers, one per line:
( echo "obase=16" ; cat file_of_integers ) | bc
Hexidecimal to decimal:
$ echo $((0xfee10000))
4276158464
Decimal to hexadecimal:
$ printf '%x\n' 26
1a
bash-4.2$ printf '%x\n' 4294967295
ffffffff
bash-4.2$ printf -v hex '%x' 4294967295
bash-4.2$ echo $hex
ffffffff
Sorry my fault, try this...
#!/bin/bash
:
declare -r HEX_DIGITS="0123456789ABCDEF"
dec_value=$1
hex_value=""
until [ $dec_value == 0 ]; do
rem_value=$((dec_value % 16))
dec_value=$((dec_value / 16))
hex_digit=${HEX_DIGITS:$rem_value:1}
hex_value="${hex_digit}${hex_value}"
done
echo -e "${hex_value}"
Example:
$ ./dtoh 1024
400
Try:
printf "%X\n" ${MY_NUMBER}
In my case, I stumbled upon one issue with using printf solution:
$ printf "%x" 008
bash: printf: 008: invalid octal number
The easiest way was to use solution with bc, suggested in post higher:
$ bc <<< "obase=16; 008"
8
In zsh you can do this sort of thing:
% typeset -i 16 y
% print $(( [#8] x = 32, y = 32 ))
8#40
% print $x $y
8#40 16#20
% setopt c_bases
% print $y
0x20
Example taken from zsh docs page about Arithmetic Evaluation.
I believe Bash has similar capabilities.
xd() {
printf "hex> "
while read i
do
printf "dec $(( 0x${i} ))\n\nhex> "
done
}
dx() {
printf "dec> "
while read i
do
printf 'hex %x\n\ndec> ' $i
done
}
# number conversion.
while `test $ans='y'`
do
echo "Menu"
echo "1.Decimal to Hexadecimal"
echo "2.Decimal to Octal"
echo "3.Hexadecimal to Binary"
echo "4.Octal to Binary"
echo "5.Hexadecimal to Octal"
echo "6.Octal to Hexadecimal"
echo "7.Exit"
read choice
case $choice in
1) echo "Enter the decimal no."
read n
hex=`echo "ibase=10;obase=16;$n"|bc`
echo "The hexadecimal no. is $hex"
;;
2) echo "Enter the decimal no."
read n
oct=`echo "ibase=10;obase=8;$n"|bc`
echo "The octal no. is $oct"
;;
3) echo "Enter the hexadecimal no."
read n
binary=`echo "ibase=16;obase=2;$n"|bc`
echo "The binary no. is $binary"
;;
4) echo "Enter the octal no."
read n
binary=`echo "ibase=8;obase=2;$n"|bc`
echo "The binary no. is $binary"
;;
5) echo "Enter the hexadecimal no."
read n
oct=`echo "ibase=16;obase=8;$n"|bc`
echo "The octal no. is $oct"
;;
6) echo "Enter the octal no."
read n
hex=`echo "ibase=8;obase=16;$n"|bc`
echo "The hexadecimal no. is $hex"
;;
7) exit
;;
*) echo "invalid no."
;;
esac
done
This is not a shell script, but it is the cli tool I'm using to convert numbers among bin/oct/dec/hex:
#!/usr/bin/perl
if (#ARGV < 2) {
printf("Convert numbers among bin/oct/dec/hex\n");
printf("\nUsage: base b/o/d/x num num2 ... \n");
exit;
}
for ($i=1; $i<#ARGV; $i++) {
if ($ARGV[0] eq "b") {
$num = oct("0b$ARGV[$i]");
} elsif ($ARGV[0] eq "o") {
$num = oct($ARGV[$i]);
} elsif ($ARGV[0] eq "d") {
$num = $ARGV[$i];
} elsif ($ARGV[0] eq "h") {
$num = hex($ARGV[$i]);
} else {
printf("Usage: base b/o/d/x num num2 ... \n");
exit;
}
printf("0x%x = 0d%d = 0%o = 0b%b\n", $num, $num, $num, $num);
}
For those who would like to use variables, first export it by running:
export NUM=100
Then run:
printf "%x\n" $NUM
Else, you can you can ignore the use case of the variables and run it directly as shown below:
printf "%x\n" 100
NB:Substitute NUM with the variable name of your choice.
Exporting makes it an environmental variable(global).
Wow, I didn't realize that printf was available at the shell!
With that said, I'm surprised no-one commented about putting the printf into a shell script (which then you could put in your personal bin directory if you wanted).
echo "printf "0x%x\n" $1" > hex
chmod +x hex
Now just run:
./hex 123
It returns:
0x7b

Resources