Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: variables in bash scripts - I'm confused

  1. #1
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    673
    Distro
    Ubuntu 14.04 Trusty Tahr

    variables in bash scripts - I'm confused

    Hi Guys,

    I am attempting to learn a bit about writing bash scripts.
    The test folder I'm using has 20 files named 1.jpg 2.JPG 3.jpg 4.JPG etc.

    I was thinking that I'd like to have a script check if any files with the .JPG extension are in a folder and then tell me yes there are or no there are not.

    If I run:

    Code:
    #!/bin/bash
    imagejpg=.JPG
    if $imagejpg; then
    echo "Files Exist" 
    fi
    I get:
    Code:
    line 6: .JPG: command not found
    If I change the script adding ".JPG" (adding the quotes)
    I get the same result.

    If I change to imagejpg="*.JPG"
    I get :
    Code:
    line 6: 10.JPG: command not found
    I would really appreciate it if someone could tell me what I am doing wrong.
    Last edited by GrouchyGaijin; April 18th, 2013 at 10:44 PM. Reason: Who says Linux guys are not friendly? This one is SOLVED.
    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. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: variables in bash scripts - I'm confused

    Code:
    imagejpg=.JPG
    for file in *$imagejpg
    do
      if [[ -f $file ]]
      then
        echo Files exist
        break
      fi
    done
    See Introduction to if, The for loop, and File name expansion

    Alternatively, you can do it like this
    Code:
    imagejpg=.JPG
    stat -c '' *$imagejpg &>/dev/null && echo Files exist
    See stat(1), redirection, and List Constructs
    Last edited by schragge; April 18th, 2013 at 10:02 AM.

  3. #3
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    673
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: variables in bash scripts - I'm confused

    Thank you!
    I'll check those links.
    You make it look so easy, pretty cool.
    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

  4. #4
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    673
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: variables in bash scripts - I'm confused

    I'm back to bug the forum again.

    OK

    The eventual goal is that when the script is run:
    The user will see that the script is looking for a specific type of image file.
    The user will be told if that file type exists or not.
    If the file type exists the user will be told that all files of that type are being resized.

    I have this which is incorrect:

    Code:
    imagejpg=.jpgread -p "Press the [Enter] key to search for jpegs..."
    for file in *$imagejpg
    do
      if [[ -f $file ]]
      then
        echo We have jpgs
    else
    echo No jpegs
    fi
    
    
    if [[ -f $file ]]
    then
    echo "Resizing jpgs"
    break
     fi
    
    
    for file in *$imagejpg
    
    
    do gm mogrify -resize 720x720 *.jpg
    
    
    break
    done
    This is the output in the terminal when I run the script:
    Code:
    Press the [Enter] key to search for jpegs...
    /home/john/scripts/test11: line 44: syntax error: unexpected end of file
    There is no mention that the directory I'm in has no jpg files.

    What am I doing wrong?
    I really appreciate the help!!
    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

  5. #5
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: variables in bash scripts - I'm confused

    Why not just
    Code:
    gm mogrify -verbose -resize 720x720 *.jpg

  6. #6
    Join Date
    Oct 2011
    Location
    ZZ9 Plural Z Alpha
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: variables in bash scripts - I'm confused

    Quote Originally Posted by schragge View Post
    Why not just
    Code:
    gm mogrify -verbose -resize 720x720 *.jpg
    Well, this, really. No need for anything else.

    Quote Originally Posted by GrouchyGaijin View Post
    I'm back to bug the forum again.

    OK

    The eventual goal is that when the script is run:
    The user will see that the script is looking for a specific type of image file.
    The user will be told if that file type exists or not.
    If the file type exists the user will be told that all files of that type are being resized.

    I have this which is incorrect:

    Code:
    imagejpg=.jpgread -p "Press the [Enter] key to search for jpegs..."
    for file in *$imagejpg
    do
      if [[ -f $file ]]
      then
        echo We have jpgs
    else
    echo No jpegs
    fi
    
    
    if [[ -f $file ]]
    then
    echo "Resizing jpgs"
    break
     fi
    
    
    for file in *$imagejpg
    
    
    do gm mogrify -resize 720x720 *.jpg
    
    
    break
    done
    This is the output in the terminal when I run the script:
    Code:
    Press the [Enter] key to search for jpegs...
    /home/john/scripts/test11: line 44: syntax error: unexpected end of file
    There is no mention that the directory I'm in has no jpg files.

    What am I doing wrong?
    I really appreciate the help!!
    First, your script is full of syntax errors- read the Bash Beginner's Guide closely- check out each section that deals with a command you use.
    As far as how to go about doing this, here's perhaps an improved way (although I'm a scripting beginner myself- someone can feel free to critique this method):

    Code:
    echo "Pres Enter to search for JPEGS"
    read start
    pics=$(find -maxdepth 1 -regex ".*\.jpe?g" -printf "%f\n")
    if [ $pics ] ; then
       echo "Pics found!"
       echo "Resizing..."
       for i in $pics ; do
            gm mogrify -resize 720x720 $i
       done
    else
        echo "No pics"
    fi
    Last edited by cortman; April 18th, 2013 at 07:16 PM.

  7. #7
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    673
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: variables in bash scripts - I'm confused

    Thank you for the reply. I'll read the guide.
    I tried the code you wrote:
    Code:
    echo "Pres Enter to search for JPEGS"
    read startpics=$(find -maxdepth 1 -regex ".*\.jpe?g" -printf "%f\n")if [ $pics ] ; then   echo "Pics found!"   echo "Resizing..."   for i in $pics ; do       do gm mogrify -resize 720x720 $i   doneelse    echo "No pics" fi
    and got this:
    Code:
    line 10: syntax error near unexpected token 
    `do' line 10: `       do gm mogrify -resize 720x720 $i'
    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

  8. #8
    Join Date
    Oct 2011
    Location
    ZZ9 Plural Z Alpha
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: variables in bash scripts - I'm confused

    The guide is good; I reference it all the time myself.
    The problem with the gm mogrify command is that I copy/pasted your line, and I already had a "do" in my for loop, so just delete the "do" and it should work. My mistake!

  9. #9
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    673
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: variables in bash scripts - I'm confused

    and it does - how cool is that!
    What is the ? in ".*\.jpe?g" for?
    How would I change it for images with the extension .JPG or .png?
    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

  10. #10
    Join Date
    Apr 2012
    Beans
    5,407

    Re: variables in bash scripts - I'm confused

    IMO you shouldn't use
    Code:
    if [ $pics ]
    or

    Code:
    for i in $pics
    Both will break if your search finds any filenames with spaces e.g.

    Code:
    $ ls *.jpg
     978-1-234 5678-9.jpg  978123456789.jpg
    $ pics=$(find . -maxdepth 1 -name '*.jpg' -printf "%f\n")
    $ if [ $pics ] ; then echo 'Pics found!'; fi
    -bash: [: 978-1-234: binary operator expected
    $
    $ for pic in $pics; do echo $pic; done
    978123456789.jpg
    978-1-234
    5678-9.jpg
    A more bullet proof way to do this kind of loop-over-find-results is along the lines of (fill in whatever you want to 'do' instead of echo)

    Code:
    $ while read -rd $'\0' pic; do echo "$pic"; done < <(find . -maxdepth 1 -name '*.jpg' -print0)
    You don't really need to test if there are any matches but if you really must then it's better to (a) quote the variable (make it a habit - ALWAYS!) and (b) test to see if the string is non-empty

    Code:
    $ if [ -n "$pics" ] ; then echo 'Pics found!'; fi
    Pics found!
    $

Page 1 of 3 123 LastLast

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
  •