Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to understand bash syntax a bit better, and I need some help with a while loop:
The following script works:
#!/bin/bash
#
counter=2
mystring=testdir
while [ $counter -le 5 ]; do
echo Making dir $mystring$counter
mkdir $mystring$counter
ls *.slurm > $mystring$counter/testfile.$counter.slurm
counter=$((counter+1))
done
Question 1: what is -le ? Google didn't seem to help, showing me any page with the word 'linux' in it
I literally copied the following form the while manual, and it does not work:
#!/bin/bash
set x 0
while {$x<10} {
puts "x is $x"
incr x
}
#test3.sh: line 6: syntax error near unexpected token `}'
#test3.sh: line 6: `}'
Question 2: What am I doing wrong? Thank you for your attention :)
The [ is a synonym for test builtin function and -le is one of the possible tests, lower or equal. In bash try this for more details:
help [
help test
Re. your second question, it doesn't look like bash syntax at all.
what is -le ?
it means "less than or equal".
What am I doing wrong?
Almost everything. Your script should look like this in bash
x=0
while [ $x -lt 10 ]; do
echo "x is $x"
((x++))
done
I think you really should read the bash manual.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The program takes input but show the following errors, how to resolve those errors ?
[ is not mere syntax, it's a command, therefore it needs space to separate it from its arguments.
while [ $counter -le $number ]
# .....^....................^
Make sure you validate that number is actually only digits.
Some other comments:
Use $(...) instead of `...` -- see
https://github.com/koalaman/shellcheck/wiki/SC2006
for more details.
bash can do arithmetic, you don't need to call out to expr. See
Arithmetic Expansion
and Shell Arithmetic
in the manual.
There is also an arithmetic conditional construct (analogous to the
string-oriented [[...]] conditional construct) -- see
Conditional Constructs
and scroll down to ((...)) (there's no direct link).
I think you need to add a space after [ and before ]:
#!/bin/bash
echo "Enter the number to find it's factorial"
read number
total=1
counter=1
while [ $counter -le $number ];
do
total=` expr $counter \* $total`
counter=` expr $counter + 1`
done
echo $total
Working here on Ubuntu:
$ ./factorial.sh
Enter the number to find it's factorial
5
120
Actually, on line 7, you forgot the completing backtick.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
A simple question :
What is the advantage of a test using a test operator versus one directly testing the variable?
As follows :
[ -n "$foo" ] && echo "that's some variable :"$foo || echo '$foo is blank.'
versus
[ $foo ] && echo "that's some variable :"$foo || echo '$foo is blank.'
The second is more elegant. But I suspect there must be a reason, because someone went to the trouble of making "-z" and "-n" exist.
I suspect [ "$foo" ] (quote the parameter expansion) is supported for historical reasons; once upon a time, saving two bytes by omitting the explicit, commonly used, -n could be significant.
Today, I would always use -n explicitly for clarity.
Note that you should not combine && and || for a "one-line" conditional statement. Use an if statement.
if [ -n "$foo" ]; then
echo "that's some variable: $foo"
else
echo "foo is blank."
fi
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I'm learning Bash and I have no idea why my if statement isn't working (working in putty if it makes any difference). I've tried looking online for how an if statement is supposed to be built and followed it making sure I got all the details correct. When I run it both of the if's come out positive and both of them get executed rather than just 1.
echo -n "Enter file name: "
read x
echo "file $x with numbers (Y/N)?"
read y
if [ "$y"="n" ];
then
cat $x
fi
if [ "$y"="y" ];
then
cat -n $x
fi
exit
can anyone help please?
Remember there needs to be a whitespace in your if condition else it becomes an assignment. https://uvesway.wordpress.com/2013/03/11/some-whitespace-pitfalls-in-bash-programming/
"$y"="n" is neither a conditional test nor an assignment. It's just a single string e.g, if $y is 'y', then "$y"="n" is the same as the literal 'y=n' and this literal string is being treated as a single parameter to the test ([) command.
A condition like below is always evaluated to true:
if [ abcd ]; then echo true; fi
while an empty string makes it false:
if [ '' ]; then echo true; else echo false; fi
White spaces aren't optional (everywhere) in bash. Even though the [ abcd thingy looks like a syntax, it's not; [ is a command (test) and abcd is its first positional parameter.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Need a help on unix shell scripting. As I am new to this.
I have a shell script written. While running that script am giving argument as say 003. I need to replace this argument value in a particular line in shell scripts as below.
Script:
if [[ $# != 1 ]]
then
echo "Please enter the Value"
echo "eg: script.sh 003"
exit 0;
fi
Q=WMS.XXX.vinoth
I need to replace XXX value with 003 and append into a temp file. Can you please help me???
Thanks in advance!!!
Do you ask how to replace the XXX with first argument?
Q=WMS.$1.vinoth
Using the $1 will work, however be aware that you should probably use quotation marks when passing a number like 003 in stead of 3 as in some cases the two zeros in front might be dropped.
Also I recommend wrapping the string in quotation marks as well, avoiding accidental command calls.
./script "003"
if [[ $# != 1 ]]
then
echo "Please enter the Value"
echo "eg: script.sh 003"
exit 0;
fi
Q="WMS.$1.vinoth"
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am getting an error ./test.sh: line 13: [: missing `]' in the file test.sh
I tried using brackets and other options such as -a or by checking the size of the file p1 but the error is always there and the else statement is always executed irrespective of the input given.I even tried by removing the ; in line 13 but it didn't help.
test.sh
#!/bin/bash
echo "Enter app name"
read y
$y &
top -b -n 1 > topLog.log
#-w checks for the whole word not and sub string from that word
grep -w "$y" topLog.log > p1
#-s option checks if the file p1 is present or not
if [ -s "p1"]; #line 13
then
echo "Successful "
else
echo "Unsuccessful"
fi
rm p1
I am new to bash scripting.So if there is any silly mistake please excuse me.
Change
if [ -s "p1"]; #line 13
into
if [ -s "p1" ]; #line 13
note the space.
I got this error while trying to use the && operator inside single brackets like [ ... && ... ]. I had to switch to [[ ... && ... ]].
You're missing a space after "p1":
if [ -s "p1" ];
add a space before the close bracket
If you created your script on windows and want to run it on linux machine, and you're sure there is no mistake in your code, install dos2unix on linux machine and run dos2unix yourscript.sh. Then, run the script.