Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Test if a file extension is supplied for script

  1. #1
    Join Date
    Aug 2016
    Location
    El Lago, Texas
    Beans
    674
    Distro
    Ubuntu Mate

    Test if a file extension is supplied for script

    How can I test if .jpg file extension is supplied?

    Code:
    #!/bin/bash
    # 
    #----------------------------------------------------------------------------
    # Convert a jpg file(that is a scan of text like a book page) to a text file
    #  Image MUST be a jpg file !!
    #
    #----------------------------------------------------------------------------
    
    #!/bin/bash
    clear
    [ -z "$1" ] && {
    echo 
    echo -e "Error!! NO JPG SPECIFIED. !!"
    echo
    echo -e "Convert jpg to text file."
    echo 
    echo -e "Image2File.sh [name of jpg file WITHOUT file extension.]"
    echo
    exit 1; }
    convert "$1.jpg" "$1.tiff" &&
    tesseract "$1.tiff" "$1"
    rm *.tiff
    Last edited by raleigh3; August 7th, 2020 at 06:14 PM.
    Ubuntu Mate 18.04 Bionic
    Mate Desktop

  2. #2
    Join Date
    May 2010
    Beans
    664

    Re: Test if a file extension is supplied for script

    Something like this:
    Code:
    if [ ${file: -4} == ".jpg" ]
    then
    #      do stuff here
    fi
    Last edited by ActionParsnip; August 7th, 2020 at 04:18 PM. Reason: closing code section

  3. #3
    Join Date
    May 2010
    Beans
    664

    Re: Test if a file extension is supplied for script


  4. #4
    Join Date
    Aug 2013
    Beans
    72
    Distro
    Lubuntu

    Lightbulb Re: Test if a file extension is supplied for script

    Quote Originally Posted by raleigh3 View Post
    How can I test if a file extension is supplied?

    Code:
    #!/bin/bash
    # 
    #----------------------------------------------------------------------------
    # Convert a jpg file(that is a scan of text like a book page) to a text file
    #  Image MUST be a jpg file !!
    #
    #----------------------------------------------------------------------------
    
    #!/bin/bash
    clear
    [ -z "$1" ] && {
    echo 
    echo -e "Error!! NO JPG SPECIFIED. !!"
    echo
    echo -e "Convert jpg to text file."
    echo 
    echo -e "Image2File.sh [name of jpg file WITHOUT file extension.]"
    echo
    exit 1; }
    convert "$1.jpg" "$1.tiff" &&
    tesseract "$1.tiff" "$1"
    rm *.tiff
    Try this:
    Code:
    EXTENSION=$( echo $1|egrep -o ".[a-z]+$" )
    if [ -z "$EXTENSION" ] then
       # no extension provided
    else
       # extension is $EXTENSION
    fi
    Budgie and loving it.

  5. #5
    Join Date
    Jan 2006
    Location
    Sunny Southend-on-Sea
    Beans
    7,925
    Distro
    Kubuntu 18.04 Bionic Beaver

    Re: Test if a file extension is supplied for script

    As another thing to consider; whether a file contains JPEG data is actually unrelated to what its name is. It's only by convention that JPEG files' names end with jpg, or jpeg, or JPG, or...

    You could instead test for what type of file you're manipulating, rather than what it's called.
    Last edited by CatKiller; August 7th, 2020 at 05:18 PM.

  6. #6
    Join Date
    Oct 2005
    Location
    Al Ain
    Beans
    10,082

    Re: Test if a file extension is supplied for script

    The file name is not a reliable indication of whether it is a JPEG file. In UNIX, a file can have any name. A JPEG file can be named picture or picture.txt or picture.gif for that matter.

    The correct way to test for a file type is with a utility called... wait for it... drum roll... file.

    Read 'man file'.

  7. #7
    Join Date
    Aug 2016
    Location
    El Lago, Texas
    Beans
    674
    Distro
    Ubuntu Mate

    Re: Test if a file extension is supplied for script

    I changed my post.

    How can I test if .jpg file extension is supplied?
    Ubuntu Mate 18.04 Bionic
    Mate Desktop

  8. #8
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    15,292
    Distro
    Kubuntu 20.04 Focal Fossa

    Re: Test if a file extension is supplied for script

    Code:
    #!/bin/bash
    
    if [ $(echo "$1" | grep \.jpg$) != "" ]
    then
       # ends in .jpg
       do stuff
    else
       # doesn't end in .jpg
       do other stuff
    fi
    The regular expression "\.jpg$" uses the $ character to denote the end of the string. The period is "escaped" with \ because it is to be taken literally. (Usually "." in a regex means any character.)
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  9. #9
    Join Date
    Apr 2020
    Beans
    67

    Re: Test if a file extension is supplied for script

    --Don't forget to check CAPitalized extensions and jpeg/JPEG variants

  10. #10
    Join Date
    Aug 2016
    Location
    El Lago, Texas
    Beans
    674
    Distro
    Ubuntu Mate

    Re: Test if a file extension is supplied for script

    Quote Originally Posted by SeijiSensei View Post
    Code:
    #!/bin/bash
    
    if [ $(echo "$1" | grep \.jpg$) != "" ]
    then
       # ends in .jpg
       do stuff
    else
       # doesn't end in .jpg
       do other stuff
    fi
    The regular expression "\.jpg$" uses the $ character to denote the end of the string. The period is "escaped" with \ because it is to be taken literally. (Usually "." in a regex means any character.)
    I am getting this message.

    Code:
    /home/andy/bin/test.sh: line 3: [: !=: unary operator expected
    continuing.
    Code:
    #!/bin/bash
    
    if [ $(echo "$1" | grep \.jpg$) != "" ]
    then
       # ends in .jpg
       echo "You included a file extension."
    else
       # doesn't end in .jpg
       echo "continuing."
    fi
    Ubuntu Mate 18.04 Bionic
    Mate Desktop

Page 1 of 2 12 LastLast

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
  •