What is wrong with this conditional statement in bash? - 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

Related

Shell scripting did not work in bamboo - how can I get latest build version

I have a code like this which is working my local computer:
__latest_version=
for dir in $(ls -1 "${RAW_BUILD_PATH}\\${release_major_version}"); do
IFS='.' read -r -a array <<< "$dir"
if [[ ${array[0]} =~ ^[0-9]+$ ]] && [[ ${array[1]} =~ ^[0-9]+$ ]]; then
if [[ "${array[0]}${array[1]}" != "${__latest_version[0]}${__latest_version[1]}" ]]; then
__latest_version[0]="${array[0]}";
__latest_version[1]="${array[1]}";
fi
fi
done
Result should be something like this:
18.1.2.3
But when I want to run this as inline script in bamboo it doesnt work.
Errors are like:
error 04-Apr-2018 15:10:02 Missing opening '(' after keyword 'for'.
error 04-Apr-2018 15:10:02 Missing file specification after redirection operator.
error 04-Apr-2018 15:10:02 The '<' operator is reserved for future use.
...

[[Bash]] Search for combined Expressions in every row

I am very new to Bash Scripting and I have a question regarding my CheckOurCodingRules.sh script:
I want to search for every 'hPar,' in a textfile and if found it should be checked if there is a also a 'const' in the same row.
Thats what I got so far but there is something wrong here:
while read line
do
if [[ $line == *hPar\,* ]] && [[ $line == *const\*]];then
DOCUMENTATION_TEST_A=1
else
echo DOCUMENTATION_TEST_A=0
fi
done < $INPUT_FILE
if [[DOCUMENTATION_TEST_A=0]];then
echo "error: Rule1: No const before hpar"
fi
There are a couple of issues with your script, see the code below which works for me:
DOCUMENTATION_TEST_A=0 # initial value
while read line
do
# spaces between conditional and brackets, no backslashes
if [[ $line == *hPar,* ]] && [[ $line == *const* ]]
then
DOCUMENTATION_TEST_A=1
break # optional, no need to scan the rest of the file
fi
done < $INPUT_FILE
# spaces and $, -eq is used for numerical comparisons
if [[ $DOCUMENTATION_TEST_A -eq 0 ]];
then
echo "error: Rule1: No const before hpar"
fi
A cleaner solution would be to use grep:
if ! grep "hPar," $INPUT_FILE | grep "const" >/dev/null
then
echo "error: Rule1: No const before hpar"
fi

error handling considers everything an error

I made a short script that changes all files with one extension to a different extension. Both extensions are inputted by the user through command line arguments. I put in an if statement to handle errors but for some reason it considers everything an error and I am not sure why. I have pasted the script below. I am rather new to bash scripting so any help would be greatly appreciated!
if [[ "$#" == 0 ]] || [[ "$1" || "$2" != "."* ]]
then
echo "Parameters are not valid"
exit
fi
for f in *"$1"; do
name=${f%.*}
mv $f "$name$2"
done
[[ "$1" || "$2" != "."* ]] should be [[ "$1" != .* ]] || [[ "$2" != .* ]]

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.

How to check if string begins with a square bracked in bash

I need to go through my ini file and check if the line begins with a bracket, to figure out that the new config has started. Is there a way of checking this in bash? I tried
line="[test]"
if [[ "$line" =~ [.* ]]; then
echo "Got it!"
else
echo "Nothing found"
fi
but it doesn't work for me. I presume that the bracket needs to be somehow escaped, but I can't find any info as to how. Any help will be much appreciated.
To do this, you should backslash the special character [, so :
line='[test]'
if [[ $line =~ ^\[ ]]; then
echo "Got it!"
else
echo "Nothing found"
fi
EXPLANATIONS
[ is the starting character to opening a REGEX class, like [0-9]
quotes are needed everywhere but not inside bash [[ ]] tests
It's terrible to use a regexp for that! please use a bash glob instead:
if [[ $line = [* ]]; then
echo 'Got it!'
else
echo "Nothing found"
fi
Hope this helps use bash more efficiently.

Resources