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
Related
This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 8 months ago.
I have a script.sh
#!/bin/bash
for i in {1..$1}
do
echo $i
done
someone could explain me why if I try
./script.sh 10 my output is {1..10}?
expected
1
2
...
10
You're echoing the parameter that you're passing in instead of printing out the loop iteration. Try:
echo $i
This question already has answers here:
How can I compare a string to multiple correct values in Bash?
(5 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 1 year ago.
I am very new to shell scripting, and i have had this error. I am using a while loop that goes a bit like this:
while [ "$variable" =! "hello" -o "$variable" =! "hi" ]
do
echo "variable isn't hi or hello"
done
but shell then gives the error [: too many arguments
This question already has answers here:
bash arithmetic expressions with variables
(2 answers)
Closed 5 years ago.
I am writing a bash script and i get an error when i try to subtracts 2 variables. I tried many ways of declaring the subtraction as a single variable, but i am constantly getting errors. What's the right way to declare the variable? Thanks in advance.
if [ ${$packets1 - $packets2} -gt 30 ]
Here you have a working solution for Bash :
packets1=300
packets2=176
if [ $(( packets1 - packets2 )) -gt 30 ]
then
echo "This is true!"
fi
Regards!
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
im new to bash scripting, appreciate if you can help.
Im trying to write a script to compare the lines in file with integer argument.
Here is what i've got so far but i make some mistakes and get error.
#!/bin/bash
a="$1"
b="wc -l < /filepath/filename.txt"
if (( $a < $b )); then
echo "file has more lines than integer"
else
echo "file has less lines than integer"
fi
Appreciate if you can point to where i make mistake.
b="wc -l < /filepath/filename.txt"
should instead be:
b=$(wc -l < /filepath/filename.txt)
...if you want to run that command and store its output in the variable.
This question already has answers here:
Bash syntax error: unexpected end of file
(21 answers)
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 7 years ago.
I tried almost all solutions suggested here but this very simple code of mine keeps showing this error
x=1
echo $x
while [$x -le 5];
do
echo $x
x=$(($x+1))
done
:
-sh-4.1$ sh test1.sh
1
test1.sh: line 10: syntax error: unexpected end of file
line 10 is one line after the "done" command. I know it has something to do with the spaces, but there aren't any spaces in my program.
You must have spaces around [ ] to do the whole script working, so :
x=1
echo $x
while [ $x -le 5 ]; do
# ^ ^
# space space
echo $x
x=$(($x+1))
done
or with bash arithmetic :
x=1
echo $x
while ((x < 5)); do
echo $((x++))
done
try put a header into the script
#!/bin/bash
this way linux will automatically interpret the script as a bash script.
I found the problem - carriage-return characters in the file.
I was typing the script in Windows (text pad) and executing it on Linux. The editor on Windows ends lines with \r\n; when running the script on Linux, the presence of \r was creating a problem.
Using vi as editor helped me resolve this issue.