Page 3 of 3 FirstFirst 123
Results 21 to 22 of 22

Thread: variables in bash scripts - I'm confused

  1. #21
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    822
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: variables in bash scripts - I'm confused

    Quote Originally Posted by schragge View Post
    Quote it:
    Code:
    gm mogrify -resize 720x720 "$i"
    You are using shopt -s nullglob, so if there's no pics, the loop won't get executed at all. See my solution above.
    I can't thank you enough!
    I'm marking this as solved and will continue reading the guide.
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  2. #22
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    822
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: variables in bash scripts - I'm confused

    Hey Guys,

    I thought you might like to see the final script:

    Code:
    #!/bin/bash 
    echo "Unload_camera version 3: This script will remove images and videos from your camera."
    echo "Basic script by GrouchyGaijin. Heavy lifting by schragge. Additional modifications by cortman, steeldriver, and ofnuts"
    echo "Run this script from the camera"  
    now=$(date +"%d-%b-%Y-%T")
    mkdir /path/to/where/you want/the/pictures/put/$now 
    echo "Making directory to hold the photos."
    
    
    dir="/path/to/clips/directory"
    read -p "Press Enter to check if the clips directory is empty"
    if [ "$(ls -A $dir)" ]; then
         echo "Clips is not empty. Did you forget to empty it last time?"
    fi 
    echo "If files were found type yes to empty the directory"
    read x
    if [ "$x" = "yes" ]
    then
    rm -f "$dir"/* 
    else
    echo "OK, Leaving the directory as is."
    fi 
                     
    pics_dir="/path/to/$now"
    clips_dir="/path/to/clips"
    
    
    process_image() {
      echo Resizing "$1"
      gm mogrify -resize 720x720 "$1"
      echo Moving "$1" to $pics_dir
      mv "$1" "$pics_dir"
    }
    process_video() {
      echo Moving "$1" to $clips_dir
      mv "$1" "$clips_dir"
    }
    
    
    process_thumbnail() {
      echo Removing "$1" from the camera
      rm "$1"
    }
    
    
    unload() {
      case $1 in
        image) pattern='*.jpg *.JPG *.png *.PNG *.GIF *.gif';;
        video) pattern='*.MOV *.mov *.mpg *.MPG *.mpeg *.mp4';;
        thumbnail) pattern='*.THM';;
        *) echo Unknown file type, aborting...; exit 1;;
      esac
      echo Press Enter to search for $1 files; read start
    
    
      unset found
      for i in $pattern; do
        if [[ -f $i ]]; then
          [[ -v found ]] || echo ${1^}s found.
          : ${found:-set}
          process_$1 "$i"
        else
          echo Skipping $i: not a regular file
        fi
      done
      [[ -v found ]] || echo No or no additional ${1}s found.
    }
    shopt -s nullglob
    unload image
    unload video
    unload thumbnail
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

Page 3 of 3 FirstFirst 123

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •