Results 1 to 5 of 5

Thread: Script Help....UTF-8 Character converter

  1. #1
    Join Date
    Feb 2008
    Beans
    24

    Question [Solved] Script Help....UTF-8 Character converter

    Hi to all...
    I have a nautilus script which converts characters from a ISO-xxx
    to Utf8 and it working good in single file... (right click on a file and convert) my "Q" is can anyone help me to make work with batch mode (lots file lets say control and select 5-6-9-12 files and convert...) the script is below...

    #!/bin/bash
    #
    # Character Encoding Conversion Script for Nautilus
    #
    # A simple nautilus script to convert from iso-8859-7
    # character encoding to utf-8.

    from="iso-8859-7" # Replace $1 with the input encoding ex. "iso-8859-7"
    to="utf-8" # Replace $2 with the output encoding ex. "utf-8"

    thefile="$*"
    if [ $# -gt 0 ];then
    iconv -f $from -t $to "${thefile}" > "${thefile}.utf8"
    mv "${thefile}.utf8" "${thefile}"
    fi
    exit 0

    thanks in advance bros...
    my email is nikkpap@gmail.com
    Last edited by nikkpap; February 6th, 2012 at 11:21 PM. Reason: Solved by Khayyam and Vaphell.... Bravo...

  2. #2
    Join Date
    Jan 2012
    Beans
    342

    Re: Script Help....UTF-8 Character converter

    nikkpap ..

    I'm not familiar with Nautilis but I assume its will treat multiple files as "$@" (as per regular bash/shell parameter expansion). Untested .. but it should work

    Code:
    #!/bin/bash
    
    FROM_ENC="iso-8859-7"
    TO_ENC="utf-8"
    
    for i in "$@" ; do
           iconv -f ${FROM_ENC} -t ${TO_ENC} "${i}" > "${i}.utf8"
           mv "${i}.utf8" "${i}"
    done
    Last edited by Khayyam; February 6th, 2012 at 05:58 PM. Reason: removed test & quoted "$@" as per Vaphell's suggested correction

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script Help....UTF-8 Character converter

    i think the test is not necessary, for loop won't do anything with the empty set either way.
    Also double-quotes are a must ( "$@" ) - otherwise parameters/names will break on spaces.
    Last edited by Vaphell; February 6th, 2012 at 04:27 PM.

  4. #4
    Join Date
    Jan 2012
    Beans
    342

    Re: Script Help....UTF-8 Character converter

    Vaphell ...

    I'd edited and added the test condition thinking that I'd oops'd and missed copying it from the OP, damn subconcious, always has to be right, heh.

    As for the "$@", best be safe, but I'd assumed the files wouldn't have spaces, I guess perhaps fontfiles copied from Windows/OSX may. Won't hurt and so I've edited it in (and removed the test).

    Thanks for the heads-up ..
    Last edited by Khayyam; February 6th, 2012 at 05:59 PM.

  5. #5
    Join Date
    Jan 2012
    Beans
    342

    Re: Script Help....UTF-8 Character converter

    Quote Originally Posted by nikkpap
    Thank you [...] it worked.
    Good .. but you should state that in the thread rather than private messaging me, that way the question can be seen as answered. So, please now mark the thread as [SOLVED].

    BTW, this script can replace yours, as it does the same thing whether one or many files are passed to it.

    Also, this isn't strickly a "Nautilus script", Nautilus just runs the script with the selected files as it input. The script will work just as well from the command line.

    Anyhow .. your welcome

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
  •