Bash script if not processing as I would expect [duplicate] - linux

This question already has answers here:
Why equal to operator does not work if it is not surrounded by space?
(4 answers)
How to compare strings in Bash
(12 answers)
Multiplication on command line terminal
(8 answers)
Closed 3 years ago.
I am in the very early stages of learning Unix scripting. I've written a Bash script which does not generate any errors, but clearly has a logic error, as the IF test always gives the same response.
I have tried various variations on the theme of my IF, but still end up with the same result.
#!/bin/bash
declare -i number1
declare -i number2
declare -i total
declare operation
echo "Enter a, s, m or d for add, subtract, multiply or divide"
read operation
echo "Enter number 1"
read number1
echo "Enter number 2"
read number2
echo "operation="$operation
if [ $operation=='m' ]
then
total=$number1*$number2
elif [ $operation=='a' ]
then
total=$number1+$number2
elif [ $operation=='d' ]
then
total=$number1/$number2
elif [ $operation=='s' ]
then
total=$number1-$number2
fi
echo $number1 " multiplied by " $number2 " equals " $total
exit 0
It doesn't matter whether I enter a, s or d (or indeed m) in response to the first prompt, my script always does a really nice multiplication... The line
echo "operation="$operation
correctly shows the operator I've requested.
Any ideas what I've done wrong?
Many thanks

You need to add spaces around all the ==. That's it. Instead of
if [ $operation=='m' ]
you should have:
if [ $operation == 'm' ]

Related

linux shell scripting error: ./mp.sh: line 10: [: missing `]' File ] is unavailable [duplicate]

This question already has answers here:
How can I compare two floating point numbers in Bash?
(22 answers)
'&&' vs. '&' with the 'test' command in Bash
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 months ago.
I am trying to build a simple BMI calculator for my college project but I am having error while using if statements. I am new to shell scripting so I don't know much about it. I think the problem is because I am using bc command because if remove the if statements it still gives me 0 answer.
Here is my code
# --------------------BMI calculator---------------------
weight="0"
height="0"
BMI="0"
echo "Welcome to BMI calculator"
echo "Please enter your weight in kilograms: "
read weight
echo "Please enter your height in meters: "
read height
if [ "$height > 0" | bc ] & [ "$weight > 0" | bc ]
then
BMI="$weight/($height*$height)" | bc
echo "Your BMI is $BMI"
else
echo "Invalid inputs!!"
fi
Please let me know if there are any other error as well

Can someone give me clue where the mistake is [duplicate]

This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 3 years ago.
I need to get user input for a number and then write a name row by row in linux terminal that much amount of times that user inputed. Example if I lets say chose a number 2 the program will write Name 2 times row by row. I wrote some code but I cant figure where is the mistake. I think its the loop where the mistake is.
echo "Please enter a number "
read $number
for value in {$number}
do
echo "Name"
done
To read input and save it into a variable named number, do:
read number
To get the value of number, do $number or ${number}. Remove the { } in the {$number} or shift $ with {.
Just do:
echo "Please enter a number "
read number
if ! test "$number" -gt 0 2> /dev/null; then
echo "You must enter an integer greater than 0" >&2
exit 1
fi
yes Name | sed ${number}q
But don't prompt for the number. Take it as a command line argument, and just do
yes Name | sed "${1}q"
Let sed generate the error message if the parameter is invalid.
The trouble with your loop is that for value in $number takes the string $number and breaks it on whitespace (depends on IFS, actually, but let's not get bogged down by details) and iterates over each value. That is, if $number is the string 1 3 dog 5, then the loop will iterate 4 times with $value taking the values 1, 3, dog, and 5. If $number is 7, then the loop iterates exactly once. You could do for((i=0; i < $number; i++)); do ..., but that does not generate any useful error message if $number is not an integer.

Making a program that takes user input for a number 1-7 and then displays a command according to the number the user chooses. [Bash - CentOS]

As the title says, I'm trying to make a program that has a user input a number 1-7, then displays the appropriate command for each number.
The problem I'm having is finding a good way to set each number to a command.
At first, I thought about doing something like this.
OSI=$(uname -a)
echo $OSI
But the problem with that is actually implementing it into a loop. Let's say the user is prompted like so:
"Enter a number:"
The user enters the number 1, and number 1 is the OSI. Well if a user picks the number 2, it needs to display a different command and so forth.
This is a little bit too complicated for a beginner like myself. I've read through forums and different posts, but I cannot figure out the right commands to make this happen.
I tried doing something like this and it failed miserably:
#!/bin/bash
read -p "Enter a number:" n1 n2 n3 n4 n5 n6 n7
if n1=1; then
uname - a
else n2=2; "different command"
fi
I realize I'm completely garbage at bash. I'm not asking for anyone to solve this, just give me some pointers in a way that makes sense to me.
Thanks.
Give this tested version a try:
#!/bin/bash --
printf "menu items:\n 1) uname -a\n 2) date\n q) exit\n"
read -p "Enter your choice: " response
if [ -z "$response" ] ; then
printf "Choice invalid\n"
exit 1
fi
if [ "$response" = q ] ; then
exit 0
fi
if [ "$response" = 1 ] ; then
uname -a
elif [ "$response" = 2 ] ; then
date
else
printf "Choice invalid\n"
fi
As written by #EdMorton case is a better option.

0999: Value too great for base (error token is "0999")

This is a shortened-version of a script for reading 8mm tapes from a EXB-8500 with an autoloader (only 10 tapes at a time maximum) attached. It dd's in tape data (straight binary) and saves it to files that are named after the tape's 4-digit number (exmaple D1002.dat) in both our main storage and our backup. During this time it's logging info and displaying its status in the terminal so we can see how far along it is.
#!/bin/bash
echo "Please enter number of tapes: [int]"
read i
j=1
until [ $i -lt $j ]
do
echo "What is the number of tape $j ?"
read Tape_$j
(( j += 1 ))
done
echo "Load tapes into the tower and press return when the drive is ready"
read a
j=1
until [ $i -lt $j ]
do
k="Tape_$j"
echo "tower1 $j D$(($k)) `date` Begin"
BEG=$j" "D$(($k))" "`date`" ""Begin"
echo "tower1 $j D$(($k)) `date` End"
END=$j" "D$(($k))" "`date`" ""End"
echo "$BEG $END"
echo "$BEG $END"
sleep 2
(( j += 1 ))
done
echo "tower1 done"
Everything was hunky-dory until we got under 1000 (startig at 0999). Error code was ./tower1: 0999: Value too great for base (error token is "0999"). Now I already realize that this is because the script is forcing octal values when I type in the leading 0, and I know I should insert a 10# somewhere in the script, but the question is: Where?
Also is there a way for me to just define Tape_$j as a string? I feel like that would clear up a lot of these problems
To get the error, run the script, define however many tapes you want (at least one, lol), and insert a leading 0 into the name of the tape
EXAMPLE:
./test
Please enter number of tapes: [int]
1
What is the number of tape 1?
0999
./test: 0999: Value too great for base (error token is "0999")
You don't want to use $k as a number, but as a string. You used the numeric expression to evaluate a variable value as a variable name. That's very bad practice.
Fortunately, you can use variable indirection in bash to achieve your goal. No numbers involved, no error thrown.
echo "tower1 $j ${!k} `date` Begin"
BEG=$j" "D${!k}" "`date`" ""Begin"
And similarly in other places.

Bash for loop parameter unexpected behaviour [duplicate]

This question already has answers here:
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 8 years ago.
I'm making a program in bash that creates a histoplot, using numbers I have created. The numbers are stored as such (where the 1st number is how many words are on a line of a file, and the 2nd number is how many times this amount of words on a line comes up, in a given file.)
1 1
2 4
3 1
4 2
this should produce:
1 #
2 ####
3 #
4 ##
BUT the output I'm getting is:
1 #
2 #
3 #
4 #
however the for loop is not recognising that my variable "hashNo" is a number.
#!/bin/bash
if [ -e $f ] ; then
while read line
do
lineAmnt=${line% *}
hashNo=${line##* }
#VVVV Problem is this line here VVVV
for i in {1..$hashNo}
#This line ^^^^^^^ the {1..$hashNo}
do
hashes+="#"
done
printf "%4s" $lineAmnt
printf " $hashes\n"
hashes=""
done < $1
fi
the code works if I replace hashNo with a number (eg 4 makes 4 hashes in my output) but it needs to be able to change with each line (no all lines on a file will have the same amount of chars in them.
thanks for any help :D
A sequence expression in bash must be formed from either integers or characters, no parameter substitutions take place before hand. That's because, as per the bash doco:
The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.
In other words, brace expansion (which includes the sequence expression form) happens first.
In any case, this cries out to be done as a function so that it can be done easily from anywhere, and also made more efficient:
#!/bin/bash
hashes() {
sz=$1
while [[ $sz -ge 10 ]]; do
printf "##########"
((sz -= 10))
done
while [[ $sz -gt 0 ]]; do
printf "#"
((sz--))
done
}
echo 1 "$(hashes 1)"
echo 2 "$(hashes 4)"
echo 3 "$(hashes 1)"
echo 4 "$(hashes 2)"
which outputs, as desired:
1 #
2 ####
3 #
4 ##
The use of the first loop (doing ten hashes at a time) will almost certainly be more efficient than adding one character at a time and you can, if you wish, do a size-50 loop before that for even more efficiencies if your values can be larger.
I tried this for (( i=1; i<=$hashNo; i++ )) for the for loop, it seems to be working
Your loop should be
for ((i=0; i<hashNo; i++))
do
hashes+="#"
done
Also you can stick with your loop by the use of eval and command substitution $()
for i in $(eval echo {1..$hashNo})
do
hashes+="#"
done

Resources