This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
I am new to shell scripting
Below is my script
#!/bin/bash
first_num = 0
second_num = 0
echo -n "Enter the first number =>"
read first_num
echo -n "Enter the second number =>"
read second_num
echo "first + second = $((first_num + second_num))"
Whenever I run it, it prints like below
/Users/haani/arithmetic.bash: line 3: first_num: command not found
/Users/haani/arithmetic.bash: line 4: second_num: command not found
Enter the first number =>
What could be the reason for command not found here?
try without spaces:
first_num=1
Related
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
This question already has answers here:
How do you execute an arbitrary native command from a string?
(4 answers)
Closed 6 months ago.
Let's say I want to get a specific dir contents:
$cmd = "dir"
$args = ".yarn*"
$output = "$cmd $args"
echo $output
My output contains 2 string literals:
dir .yarn*
Expected (as a command on 1 line, opposed to a "string" on separate lines):
dir *yarn
#iRon 's comment with a suggested answer was correct:
Invoke-Expression($output) is the answer!
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.
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' ]
This question already has answers here:
Check if a variable exists in a list in Bash
(21 answers)
Closed 5 years ago.
I have a bash script and I want to check if a string is in a list.
Like: string = "Hello World!", List=("foo", "bar").
Python example:
if name in list: # Another way -> if name in ["foo", "bar"]
# work to do
else:
sys.exit(1)
Thank you!
There are a number of ways, the simplest I see is:
#!/bin/sh
WORD_LIST="one two three"
MATCH="twox"
if echo "$WORD_LIST" | grep -qw "$MATCH"; then
echo "found"
else
echo "not found"
exit 1
fi