Results 1 to 10 of 10

Thread: Shell-Script: I want to rip off the 4 last characters of a var.

  1. #1
    Join Date
    Jan 2008
    Beans
    184

    Shell-Script: I want to rip off the 4 last characters of a var.

    I'm making a shell script to efficiently "thumbnalize" lotsa PNG images and convert them into JPG and also watermark their respective PNG sources. The problem is, after some "runtime", i need to delete the ".png" at the end of the variable "$img" (ls *png) , so i can save it as "something.jpg" and not "something.png.jpg".
    I tried to use cut and sed but they don't work with variables (i think), a workaround is to "rename" the jpg images after saving them... but i would like to just remove the last 4 char of the variable at runtime and save them correctly.

    Thank you in advance.

  2. #2
    Join Date
    Jan 2005
    Location
    Nashville
    Beans
    3
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Shell-Script: I want to rip off the 4 last characters of a var.

    You might be able to use something like

    basename filename.png .png

  3. #3
    Join Date
    Jan 2008
    Beans
    184

    Re: Shell-Script: I want to rip off the 4 last characters of a var.

    Exactly what I need! Thank you very much!!

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Shell-Script: I want to rip off the 4 last characters of a var.

    no need to use external command. in shell
    Code:
    for filename in *.png
    do
      echo ${filename%.png}
    done

  5. #5
    Join Date
    Jan 2008
    Beans
    184

    Re: Shell-Script: I want to rip off the 4 last characters of a var.

    Quote Originally Posted by ghostdog74 View Post
    no need to use external command. in shell
    Code:
    for filename in *.png
    do
      echo ${filename%.png}
    done
    COOL!!

    Many thanks!

  6. #6
    Join Date
    Feb 2008
    Location
    Lancashire, UK
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell-Script: I want to rip off the 4 last characters of a var.

    Quote Originally Posted by Repgahroll View Post
    I tried to use cut and sed but they don't work with variables (i think)
    If you want to use cut you could do this sort of thing:
    Code:
    for pngfile in *.png; do
        echo $(echo $pngfile | cut -d. -f1).jpg
    done

  7. #7
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Shell-Script: I want to rip off the 4 last characters of a var.

    Quote Originally Posted by John Bean View Post
    If you want to use cut you could do this sort of thing:
    Code:
    for pngfile in *.png; do
        echo $(echo $pngfile | cut -d. -f1).jpg
    done
    no need to use external tools. since the shell can do this, using the shell is much faster ( although to some, they do not care.)

  8. #8
    Join Date
    Feb 2008
    Location
    Lancashire, UK
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell-Script: I want to rip off the 4 last characters of a var.

    Quote Originally Posted by ghostdog74 View Post
    no need to use external tools. since the shell can do this, using the shell is much faster ( although to some, they do not care.)
    I'm perfectly aware of that. My reply was in the context of the part of the OP I quoted, to demonstrate the error in his assumption that "cut doesn't work with variables". Although it's not needed or desirable in this case, the use of echo output piped into a filter like cut is a valid (if often abused) technique.

    This sort of query deserves a number of possible solutions in order to learn anything from it. Choice is good.

  9. #9
    Join Date
    Feb 2009
    Beans
    90

    Re: Shell-Script: I want to rip off the 4 last characters of a var.

    @John Bean
    But that doesn't work if your png is named
    Code:
    dotted.image.filename.png

  10. #10
    Join Date
    Feb 2008
    Location
    Lancashire, UK
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell-Script: I want to rip off the 4 last characters of a var.

    Quote Originally Posted by raffaele181188 View Post
    @John Bean
    But that doesn't work if your png is named
    Code:
    dotted.image.filename.png
    Sigh. Let me say it one more time: the OP wrote "I tried to use cut and sed but they don't work with variables", so I used an example to show him that his assumption was incorrect. He may really need to use a similar construct at some point, so it's useful to know how to use variables with filters like cut.

    The most appropriate solution to his problem using the shell's inbuilt functions had already been given.

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
  •