How to check number of arguments [duplicate] - bash

This question already has answers here:
Difference between sh and Bash
(11 answers)
Double parenthesis with and without dollar
(2 answers)
Closed 17 days ago.
I made a simple script, just to test if the user passed more than 2 arguments.
My script is the following
#!/bin/sh
if (( $# > 2 )); then
echo "greater"
else
echo "not greater"
fi
It should be really simple, since $# returns the number of arguments, but I receive the following error:
schwarz#peano:~$ ./test.sh
./test.sh: 3: 0: not found
not greater
schwarz#peano:~$ ./test.sh one
./test.sh: 3: 1: not found
not greater
schwarz#peano:~$ ./test.sh one two
./test.sh: 3: 2: not found
not greater
schwarz#peano:~$ ./test.sh one two three
./test.sh: 3: 3: not found
not greater
schwarz#peano:~$ ./test.sh one two three four
./test.sh: 3: 4: not found
not greater
It says "not found" to the number of arguments I pass. What I'm doing wrong?
Thanks in advance.

Related

Shell Scripting help stuck in unary operator error [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
Hey guys I am trying to teach myself shell scripting, but I am stuck in a error and I am just unsure how to fix it. I have created a simple while loop that should just give a output from 0-9. This is the error I receive
./loops.sh: line 10: 0: command not found
./loops.sh: line 11: [: -lt:unary operator expected
Here is my code
#while loop
x= 0
while [ $x -lt 10 ]
do
echo $x
x= 'expr $x + 1'
done

Increment variable name results in command not found

I am trying to increment a variable name based on the input and call the value after the loop is done.
for i in `seq 5`; do
var$i="number is $i"
done
echo $var2 $var5
Results in
./test.sh: line 4: var1=number is 1: command not found
./test.sh: line 4: var2=number is 2: command not found
./test.sh: line 4: var3=number is 3: command not found
./test.sh: line 4: var4=number is 4: command not found
./test.sh: line 4: var5=number is 5: command not found
There are 2 things I don't understand:
"var1=number is 1" is understood as a command.
var2 and var5 are actually generated but they are not displayed outside of the loop.
You cannot use variable names with a number in it like that: you really need to say:
var1=1
var2=2
var3=3
var4=4
var5=5
Another approach is the usage of arrays.
As far as increasing is concerned (this is not part of the question, but I give is anyways), you might use something like this:
Prompt> var1=1
Prompt> var2=$((var1+1))
Prompt> echo $var2
2
(Mind the double brackets for making calculations)
To achieve the outcome required, the use of arrays are needed and so:
#!/bin/bash
for i in $(seq 5)
do
var[$i]="number is $i"
done
for i in "${var[#]}"
do
echo "$i"
done
Set the index and values for the array var accordingly and then loop through the array and print the values.
"var1=number is 1" command not found because that's not a command. For example, you can write:
for i in `seq 5`; do
echo var$i="number is $i"
done
And the output would look like
var1=number is 1
var2=number is 2
var3=number is 3
var4=number is 4
var5=number is 5
The variables have not been generated, you can't just dynamically generate variables. Try to use arrays

syntax error: unexpected end of file in if statement bash file [duplicate]

This question already has an answer here:
Why is a shell script giving syntax errors when the same code works elsewhere? [duplicate]
(1 answer)
Closed 5 years ago.
Source code:
#!/bin/bash
echo "Parametro is $1"
if [ $1 -gt 9 ]
then
echo "entre al if";
fi
echo"Fin del script";
My bash file has execute permission:
-rwxrwxr-x 1 mzadmin mzadmin 108 Feb 15 13:07 test.sh
I run my bash file like:
bash test.sh 9
And the output is:
Parametro is 9
test.sh: line 8: syntax error: unexpected end of file.
You need a space after echo command : echo"Fin del script";
So :
echo "Fin del script"
Next time, before asking human help, please pass your script online on http://www.shellcheck.net/ (even if this particular error is not detected, because the shell think echo"Fin del script"; is a unknown command...)
Edit
If you don't have output it's because you use in your test -gt (is greater) but instead you need -ge is greater or equal.
Better use bash arithmetic like this, it's more readable and less error prone :
if ((arg >= 9)); then
You need to put a newline at the end of your script, because sometimes things don't work properly if they can't find a newline character. See Why should text files end with a newline?

“No such file or directory” when comparing numbers in bash [duplicate]

This question already has answers here:
Less than operator '<' in if statement results in 'No such file or directory'
(3 answers)
Closed 6 years ago.
I'm getting an strange error.
#!/bin/bash
echo "Please enter a number"
read var
declare -i num
num=0
while ($num<$var)
do
echo "$num"
done
./loop: line 5: 6: No such file or directory
What am i mistaking?
This is the correct syntax:
while [ "$num" -le "$var" ]
do
echo "$num"
done
What you wrote, $num<$var, is the syntax for running a program with a file as input. Like this:
cat < file.txt
The error is telling you that $var (the content of $var, not literally "var") was not found when Bash attempted to open it as a file.

Bash re-assignment of value to variable "command not found" [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 8 years ago.
Any ideas why this is happening? Why do I have to manually explicitly reassign the variable but can't do it if I have another variable in the name of the variable?
SCRIPT:
#!/bin/bash
a_1=1
a_2=1
for temp in 1 2
do
a_$temp="2"
echo $((a_$temp))
done
a_1=2
a_2=2
echo $a_1
echo $a_2
OUTPUT:
[dgupta#della4 Rates_Of_Quenching]$ ./test.sh
./test.sh: line 8: a_1=2: command not found
1
./test.sh: line 8: a_2=2: command not found
1
2
2
Instead of:
a_$temp="2"
Use:
declare a_$temp="2"
to create variable with dynamic name.
As far as bash is concerned, you are trying to execute the command 'a_1=2', rather than perform an assignment. You can get around this by using declare, or its synonym typeset:
'a_1=2' # bash: a_1=2: command not found
typeset 'a_1=2'
echo $a_1 # 2
declare 'a_1=3'
echo $a_1 # 3
While it is possible to use declare, you might want to take advantage of bash arrays (which have been around since bash version 2) rather than using variables with numerical suffixes:
a=(1 1)
echo ${a[0]} # 1
echo ${a[1]} # 1
for i in 0 1; do a[i]=2; done
echo ${a[0]} # 2
echo ${a[1]} # 2

Resources