Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Very Short Form of Bash Script Not Working :)

The script is basically polling directory /images for jpg files and moving them to new directory /gallery/2022-06-14/ while also renaming (removing initial characters). I was planning to add another action along where if existing time is between 000000 and 050000, bash script should invoke python script by taking renamed file path as argument.

Can you tell if its not possible to use short form of if in this manner? Why its erroring out?

find '/images' -maxdepth 1 -name '*jpg' -exec sh -c '''echo mv {} /gallery/$(date --date="today" '+%F')/$(basename {} | sed "s/^.\{,19\}//") && [[ ! ( `date +"%H%M%S"` < 000000 || `date +"%H%M%S"` > 050000 ) ]] && python3 python-script.py "/gallery/$(date --date="today" '+%F')/$(basename {} | sed "s/^.\{,19\}//")"''' \;
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected
sh: 1: Syntax error: "(" unexpected

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

if its not possible to use short form of if in this manner?

It is possible.

Why its erroring out?

That specific error is most probably because you used sh instead of bash. [[ is super special for Bash with special syntax rules and it specially handles (.

Anyway, the code is unreadable, and uses an find extension and is not protected against spaces in filenames and has issues. Do not use backticks, prefer $(...). Consider a different approach, where you do not have to super double quote everything:

work() {
  # normal code here, you could use variables.
  echo mv "$1" "/gallery/$(date --date="today" '+%F')/$(basename "$1" | sed "s/^.\{,19\}//")"
  if [[ ! ( $(date +"%H%M%S") < 000000 || $(date +"%H%M%S") > 050000 ) ]]; then
     python3 python-script.py "/gallery/$(date --date="today" '+%F')/$(basename "$1" | sed "s/^.\{,19\}//")"
  fi
}
export -f work
find .... -exec bash -c 'work "$@"' _ {} \;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading