Skip to content
Advertisement

crontab shell script starts another script: remove char error

I want to run a shell script through crontab which does something and then runs another shell script which checks a certain folder for certain files and creates a directory for each file. The first script “my_crontab.sh” looks like this:

#!/bin/bash
cd /home/myusername/myfolder
python mypythonscript.py
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/myusername/myfolder
bash start.sh

Crontab executes “my_crontab.sh” and “start.sh” gets also called (I know from the crontab log file)

This is what “start.sh” looks like:

#!/bin/bash
echo "start.sh started"
if [ $(find -maxdepth 1 -type f | wc -l) -ne 0 ]; 
then
 for i in *.jpg
 do
  folder="${i/.jpg/}"
  mkdir ../folders/$folder
 done
else
 echo "nothing to do"
fi

now I’m getting the error: /bin/sh: 7: /home/myusername/myfolder/start.sh: Bad substitution

I already tried folder=${ i:0:-4 } instead, but that’s also not working. Any ideas how to fix it?

Advertisement

Answer

Continuing from the comment, your Bad Substitution in start.sh is caused because you are leaving spaces in line 7 between the braces and your variable name and substitution, (e.g. "${ i/.jpg/ }" should be "${i/.jpg/}". Line 4 is problematic because -ne expects integer expressions on both sides and there is no guarantee the command substitution will comply (it should with wc -l) but it is not guaranteed. You are better served writing start.sh something similar to:

#!/bin/bash

echo "start.sh started"
declare -i cnt=0;

while read -r fname
do
    folder="${fname%.jpg}"
    mkdir -p "../folders/$folder"
    ((cnt++))
done < <(find . -maxdepth 1 -type f -name "*.jpg")

[ "$cnt" -eq '0' ] && echo "nothing to do"

note: the counter cnt is just used to output "nothing to do" in the event there are no .jpg files found, if that isn’t needed, just delete all lines referring to cnt.

Advertisement