andre andre - 1 year ago 93
Bash Question

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 "" looks like this:

cd /home/myusername/myfolder

Crontab executes "" and "" gets also called (I know from the crontab log file)

This is what "" looks like:

echo " started"
if [ $(find -maxdepth 1 -type f | wc -l) -ne 0 ];
for i in *.jpg
mkdir ../folders/$folder
echo "nothing to do"

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

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

Answer Source

Continuing from the comment, your Bad Substitution in 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 something similar to:


echo " started"
declare -i cnt=0;

while read -r fname
    mkdir -p "../folders/$folder"
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download