Syntax error in conditional expression in shell script - shell

I write a shell script. I have a problem that I want to determine folder name manually. Actually, I solved with #Suhas's helping. When I execute my script, I have an error
./scriptname: line 5: syntax error in conditional expression
./scriptname: line 5: syntax error near `"${folder2}"'
./scriptname: line 5: ` if [[ ! -d "${folder1}" "${folder2}" ]]; then'
Can someone figure out what's wrong?

This:
if [[ ! -d "${folder1}" "${folder2}" ]]; then
needs to be separated into 2:
if [ ! -d "${folder1}" ] && [ ! -d "${folder2}" ]; then

A better & valid solution for a double test still using [[ ]] bash test :
if [[ ! -d "${folder1}" && ! -d "${folder2}" ]]; then
Note
[[ is a bash keyword similar to (but more powerful than) the [ command. See http://mywiki.wooledge.org/BashFAQ/031 and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals. Unless you're writing for POSIX sh, we(wooledge.org) recommend [[.

Related

What is wrong with this conditional statement in bash?

I am trying to check:
if $file is a binary, and the $file is not an image file then do something.
if [[ "$(file --dereference --mime "$FILE")" =~ binary ]] && [[ "$FILE" != \.jpg$|\.jpeg$|\.png$ ]]; then
echo "$1 is a binary file"
exit 0
fi
The error is a syntax error in conditional expression: unexpected token
I guess I am probably overlooking something simple. I have googled quite a bit but cannot get a working statement. Any tips are greatly appreciated.
You seem to trying to do a negate match on the second [[. You can do it by putting ! before a match =~
Here is an example that may help you:
[[ ! 'foo.png' =~ \.(jpe?g|png)$ ]] && echo not a image

Bash - syntax error near unexpected token `fi'

#!/usr/bin/env bash
if [[ $# -eq '0' ]]
then
var=command
if [[ ${var} -eq '0' ]]
then
do something
else
do something else
fi
fi
if [[ $# -eq '1' ]]
usage;
fi
if [[ $# -eq '2' ]]
if [[ "$1" != "-r" ]]
then
usage;
fi
if [[ "$2" =~ some_pattern ]]
then
do something
else
echo "Pattern is in an improper format. Please enter it as: correct_pattern, and try again"
exit 1
fi
usage="Usage: meta_script.sh -r correct_pattern
-r for reset is used to manually pass a parameter instead of using the default"
exit 1
fi
When I run this script, this is the error I get:
./meta_script.sh: line 31: syntax error near unexpected token `fi'
./meta_script.sh: line 31: `fi'
In the first if statement where I'm checking if the number of parameters are equal to 1, I had put a then, but I got the same error as above, except with then instead of fi. It's almost as if no matter what I put and where, I get these errors and when I remove them to try and fix it, I get another bunch of similar errors. Please help me correct this script. Thanks!
With regard to the segment:
if [[ $# -eq '2' ]]
if [[ "$1" != "-r" ]]
then
you are missing the then for the first if statement. Putting that in should get you past that error:
if [[ $# -eq 2 ]] ; then
if [[ "$1" != "-r" ]] ; then
You'll see I've put the then on the same line since it's a good habit to get into, realising that if and then always go together (same as while and do). It also allows you to see more lines of "useful" code on any given terminal :-)
I've also gotten rid of the useless quotes around 2 since $# always returns a numeric value. I'd advise sticking to using quotes just for strings.

Multiple `if` statements in bash script

I'm trying to write a short bash script that optionally accepts arguments from the command line, or prompts for their input
if [ [ -z "$message" ] && [ -z "$predefined" ] ] ; then
read -p "Enter message [$defaultMessage]: " message
message=${message:-$defaultMessage}
else
if [ -n "$predefined" ]; then
if [ -f $base/$environment/vle/data/$predefined.txt ]; then
echo Predefined message file $predefined.txt does not exist
exit 1
fi
fi
fi
If neither message nor predefined has been passed in as command line arguments, then the code should prompt for a value for message; otherwise if predefined has been passed in as a command line argument, then the script should test for the existence of a file of that name and only continue if the file does exist
But I'm getting the following error
[: -z: binary operator expected
at the first of those if tests
Any help in explaining what's wrong with my syntax for that first if statement? Or providing an alternative syntax to achieve the same objectives.
The first if is not well-formed. This would work:
if [ -z "$message" ] && [ -z "$predefined" ]; then
or this:
if test -z "$message" && test -z "$predefined"; then
or this bash-specific, easy but dirty way:
if [[ -z "$message" ]] && [[ -z "$predefined" ]]; then
or this bash-specific proper way:
if [[ -z $message && -z $predefined ]]; then
In this last version the double-quotes are unnecessary, not a typo.
Thanks #mklement0 for the corrections in the bash-specific style, and for this final comment:
I should note that there's one case where double-quoting is still a must inside [[ ... ]], namely if you want a variable reference on the right side of a string comparison (==) to be treated as a literal:
v='[a]'
[[ $v == $v ]] # FALSE!
[[ $v == "$v" ]] # true
Without double-quoting, the right-hand side is interpreted as a pattern. Some people advocate always double-quoting variable references so as not to have to remember such subtleties. That said (from bash 3.2 on), you must NOT double-quote the right operand when regex matching with =~
test expression1 -a expression2
is true if both expressions are true.
test expression1 -o expression2
is true if either or both expressions are true.
if [ -z "$message" -a -z "$predefined" ]; then
read -p "Enter message [$defaultMessage]: " message
message=${message:-$defaultMessage}
else
if [ -n "$predefined" -a -f $base/$environment/vle/data/$predefined.txt ]; then
echo Predefined message file $predefined.txt does not exist
exit 1
fi
fi
This was able to combine 4 test into 2 while also getting rid of one nested if expression; then ; fi

An error with if statement in shell scripting

I'm trying to write a shell script, but it's giving me a syntax error at the following command:
if [[ -n ${array[$x1]} -a [ expr length "$x1" -gt 2 ] ]]
This is the error message:
./project: line 45: syntax error in conditional expression
./project: line 45: syntax error near `-a'
./project: line 45: ` if [[ -n ${array[$x1]} -a [ expr length "$x1" -gt 2 ] ]]'
What am I doing wrong?
Use && not -a in [[ ]]
Also, expr length won't do what you expect here. The better approach, since you're already using bash extensions, is to use the ${#param} expansion to get the length of $param, and evaluate that within a math context, like so:
if [[ -n ${array[$x1]} ]] && (( ${#x1} > 2 )); then
...
fi

double bracket does not work for -d and -f commands

if [[ -d "$HOME/test_bash/$KIT.stat_$KIT" ]; then
echo $KIT
else
mkdir $KIT.stat_$KIT
fi
if [[ "14"=="14" ]]; then
echo "FOO"
fi
The first if statement with [[ -d does not work, but the second if statement "14"=="14" does work when I use the bash test.sh command. However, if I replace the first if statement '[[' with a single bracket '[', it works. Any idea why this is the case?
You only have a single closing ] on your first line.

Resources