Correct syntax of Shell script.

Discussion in 'Programming/Scripts' started by pawan, Jan 30, 2019.

  1. pawan

    pawan Member HowtoForge Supporter

    I haven't created any shell script earlier.
    Am trying to create one and add the same to the cron.
    This is the commands I am trying to run through the script.
    Code:
    #!/bin/bash
    find . -mmin -60 -name "*.jpg" $(printf "! -name %s " $(cat processed.txt) ! -name cache) -exec convert -resize 1000x800 -quality 85% {} {};
    find  -mmin -60 -type f -name "*.jpg" -exec basename {} \; &> processed.txt
    If I am running these commands directly on shell, I don't get any error.
    but if say I have stored this in a file called compress and run the script as ./compress
    I get the error -
    Code:
    find: missing argument to `-exec'
    what mistake I am making and how I can fix that.
     
  2. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Try starting script like this
    Code:
    #!/bin/bash
    set -x
    That should show what is happening.
     
  3. pawan

    pawan Member HowtoForge Supporter

    Hi Taleman,
    I add set-x and then run the script like
    sh compress
    the ouput I am getting is like
    Code:
    sh compress
    : invalid option: set: -
    set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
    
    Though I have reduced the code in my first post like:
    Code:
    #!/bin/bash
    set -x
    find . -mmin -1360 -name "*.jpg" -not \( -path "./cache" \)  -exec convert -resize 1000x800 -quality 85% {} {} \;
    
    but If I don't include set -x I am still getting the same error
    that is
    Code:
    find: missing argument to `-exec'
    but if I run the same command directly on the shell there is no error.
     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    I've moved this thread to Programming and Scripts forum.
     
  5. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Why do you have
    Code:
    {} {};
    What does two {} mean?
     
  6. pawan

    pawan Member HowtoForge Supporter

    Hi Taleman I don't have much idea as I stated, I am just seen some syntax on statck and tried to imitate that.
    BTW I got an working solution as I also posted the same query on stackoverflow. Here is the the detail which is working fine, which also contains two {} {}
    Code:
    Build an array of arguments for the first find command instead of relying on the command substitution.
    
    while IFS= read -r line; do
      processed+=(! -name "$line")
    done < processed.txt
    Your immediate problem, though, is that you forgot to escape the semicolon so that it would be treated as an argument to find, rather than a command terminator.
    
    find . -mmin -60 -name "*.jpg" "${processed[@]}"  \
      ! -name cache  -exec convert -resize 1000x800 -quality 85% {} {} \;
    #                                                                  ^^
    find  -mmin -60 -type f -name "*.jpg" -exec basename {} \; &> processed.txt
     

Share This Page