Results 1 to 2 of 2

Thread: while read and function

  1. #1
    Join Date
    Jan 2010
    Location
    England
    Beans
    185
    Distro
    Ubuntu

    while read and function

    how do i call a function into a while read script. i have a function that renames a file extension.

    Code:
    function rename {
    
     "${f%.*}.$ext" 
    
    }
    how can i call that function into a while read command

    Code:
    while read f; do echo "rename function"; done
    i am aware i could just put it like

    Code:
    while read f; do echo "${f%.*}.$ext"  ; done
    the actuall function is more complex. so i would like to be able to call it in during the while read command.

    spiritech
    Keep those cups of Ubuntu comming.
    Ubuntu 12.10

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: while read and function

    Try something like:
    Code:
    rename ()
    {
        file="$1"
        ext="$2"
        file="${file%.*}.$ext"
        ...
        echo "$file"
    }
    
    e=".whatever"
    while read -r f
    do
        new=$(rename "$f" "$e")
        mv -- "$f" "$new"
    done
    See: http://mywiki.wooledge.org/BashGuide...ands#Functions

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
  •