I am writting a shell script which will monitor the folder "/home/Folder" and it will call the script.py when it found the file format 1234567.csv file.
inotifywait -m --format '%f' -e create /home/Folder | while read FILE
do echo "$FILE is created" if [[ "${FILE: -11}" == "1234567.csv"]] then echo "correct format" python /home/Folder/script.py else echo "NOT correct format" fi
doneHowever I got "Bad substitution" error and if-else condition does not execute.
May I know is this the correct way of checking whether the FILE's last 11 digit is the same as 1234567.csv? [[ "${FILE: -11}" == "1234567.csv"]]
1 Answer
You are missing a white space within the end of the test - "1234567.csv"]], it should be:
[[ "${FILE: -11}" == "1234567.csv" ]]To find such simple mistakes you can use shellcheck online ot you can install it via apt and then do:
shellcheck script.shNote: It is important to place the correct shebang at the beginning of the script. It tells the current shell in which language the script must be interrelated.
Currently you are using some Bash features - as the double bracket test (type help [[ and help [ to see the difference) so you must specify #!/bin/bash as shebang.
If the shebang is #!/bin/sh you need to rewrite your scrip in Dash/Sh syntax. If the script was pure Python3 the shebang must be #!/usr/bin/python3 or for more portability #!/usr/bin/env python3.
If the shebang is missing the script may work within your shell in case it is a Bash script and your shell is Bash - but it will fail, for example, within crontab because by default it uses /usr/bin/sh as shell.