Bash scripting missing ']' [closed] - bash

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.

Related

need help understanding bash syntax [closed]

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.

My if statement executes both if's rather than just 1 [closed]

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.

Getting "syntax error near unexpected token `else’" in shell script [closed]

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
I’m using bash shell on Amazon Linux. When I run the below block of code
if [ $rc -eq 0 ] then
passed=`tr ' ' '\n' < $TFILE | grep -c PASSED`
error=`tr ' ' '\n' < $TFILE | grep -c ERROR`
warning=`tr ' ' '\n' < $TFILE | grep -c WARNING`
subject="Automated Results - $passed passed, $error errors, $warning warnings."
else
subject="Failed to run any tests."
fi
I get the error, “syntax error near unexpected token `else’”. What do I need to do to write this if-then-else block correctly?
To quote the syntax definition from help if in bash (which is quite close to the relevant POSIX spec):
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
There can be multiple commands used in the conditional part of an if statement, and a command separator (represented as a semicolon here) is mandatory between the last of them and the list of commands to given should that conditional part return a successful status.
Comparing the code given against that syntax definition, it's missing such a separator:
if [ $rc -eq 0 ]; then
# ^
# \- this semicolon, or a newline, is mandatory before "then"
As it is, then is being passed as an argument to the [ command, not parsed as syntax.
(Since you're tagged bash, consider also using native math syntax: if (( rc == 0 )); then is both more readable and less buggy).

How can I test if a variable contains a string in a bash script without getting the "command not found" error? [closed]

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 8 years ago.
Improve this question
I keep getting a "command not found" error after checking a variable for the substring .txt.
Here's a simple version of my script.
myscript.sh
#!/bin/sh
if [["$1" == *.txt]]
then
echo $1
fi
Result:
> ./myscript.sh argument.txt
./myscript.sh: line 2: [[argument.txt: command not found]]
The error is because of a space needed around the brackets [[ and ]]:
#!/bin/sh
if [[ "$1" == *.txt ]]
then
echo $1
fi
That is, instead of:
if [["$1" == *.txt]]
use
if [[ "$1" == *.txt ]]

Unix Shell scripting query on sed [closed]

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"

Resources