Passing Files/Folders to BASH script...

I'm trying to pass a list of files and folders selected in finder to a bash script so that I can use VLC to perform some transcoding and concatenation on them.
I'm using "Get Selected Finder Items" to pass the "Files/Folders" output "As Arguments" to my script.
Problem is with spaces in the filenames. Apparently Automator delimits the list of files with spaces. So I have spaces in the filenames and spaces between the files. So my bash script just sees a list like this:
/Users/johnt/Documents/3rd Party Audio For Conversion/WFWC - 3.6.2009 3.7.2009/01c Director Report March 6 2009.wav /Users/johnt/Documents/3rd Party Audio For Conversion/WFWC - 3.6.2009 3.7.2009/01b Comm Disc Mar 6 2009.wav
...with no way to differentiate each file. Is there a way to tell automator to escape the spaces in the file paths so that my loop doesn't try to run through $@ as:
/Users/johnt/Documents/3rd
Party
Audio
for
... etc etc
Thanks

Thanks Neil, that was helpful. I'll play with that AppleScript and see what happens.
I figured this was better suited in the Automator forum since I'm trying to format the output of what Finder sends before it ever hits the Bash script. My Bash script itself has no problem, so I didn't see any point going over to the UNIX forums.
You gotta love the ability of OSX to be able to handle all sorts of technologies and integrate them well. Apparently, there's nothing Apple can do to make it's users do the same thing.

Similar Messages

  • Bash script to remove hidden files

    Hello, I produced this script to clean folders from hidden files
    #!/bin/bash
    files=$(find . \( -iname '*~' -o -iname '.*' \) -type f)
    #count the hidden files
    n=$(echo "$files" | wc -l)
    if [ $n -eq 0 ]
    then
    echo "No hidden file found"
    exit 0
    fi
    #prompt to the user the found files
    echo "Hidden files found:"
    echo "$files"
    printf "Remove? [y/N] "
    read input
    input=$( echo $input | tr '[A-Z]' '[a-z]' )
    #take action if confirmed
    if [ "$input" == "y" ] || [ "$input" == "yes" ]
    then
    find . \( -iname '*~' -o -iname '.*' \) -type f -exec rm '{}' \;
    echo "Hidden files removed"
    else
    echo "Nothing was done"
    fi
    exit 0
    As you can see I've not been able to deleting reading from $files, but I have to call a second time the find command. So first improvement is to get rid of it (keeping managing spaces in file names correctly, of course).
    Other suggestions are welcomed!

    falconindy wrote:Or really... just use one of the several well-formed examples in this thread.
    Yeah there's one solution using arrays formulated by Trilby, and improved by aesiris. Kind of reminds me of the episode Sheldon Cooper says, "Notify the editors of the Oxford English Dictionary: the word 'plenty' has been redefined to mean 'two'."
    As for your earlier post:
    falconindy wrote:
    No, this simply doesn't work because the entirety of the find result will be quoted as a single filename. You really should just do this all in find...
    find . -name '.*' ! -type -d -exec rm -i {} +
    The + thing is a good example of saving resources, however the op doesn't want to prompt the user for authorization for every single file (rm -i). Maybe two invocations of find? By the time the user has finished reading the file list, other files may have been created and they will be removed without authorization.
    falconindy wrote:Manipulating IFS is entirely wrong for the purposes of reading a list out of a simple string variable (which is wrong on its own -- this is why arrays exist).
    I believe that calling a functioning, standards-compliant solution "entirely wrong" means to call it "not as clean as you would desire". So, back to the drawing board:
    files=$(find . -type f)
    echo "$files"|xargs -d$'\n' rm
    Although my earlier post proved one can work with IFS, you were right after all, it is not necessary. Hope you don't spot any further problems or my code will end up being a single word!

  • Using Bash script to edit config file

    This is a really simple question, but given that I'm just learning Bash scripting and having this solved now would be really illustrative for me, I would really thank some help here.
    I'm using uzbl, and running Tor+Polipo. So, as you will see below in the tail of the config file, there is a line to redirect the requests of uzbl through Polipo.
    # === Post-load misc commands ================================================
    sync_spawn_exec @scripts_dir/load_cookies.sh
    sync_spawn_exec @scripts_dir/load_cookies.sh @data_home/uzbl/session-cookies.txt
    # Set the "home" page.
    #set uri = https://duckduckgo.com
    # Local polipo proxy
    set proxy_url = http://127.0.0.1:8123
    # vim: set fdm=syntax:
    What I want to accomplish is to comment in/out that line with a key shortcut on Awesome. I've thought of doing 2 scripts to do so and using 2 differente key shortcuts, but I want to "toggle" the proxy redirection with only 1 shortcut. To do so, I suppose that the script should go something like:
    if
    tool 'set proxy_url = http://127.0.0.1:8123' config_file
    then
    tool '#set proxy_url = http://127.0.0.1:8123' config_file
    else
    if
    tool '#set proxy_url = http://127.0.0.1:8123' config_file
    then
    tool 'set proxy_url = http://127.0.0.1:8123' config_file
    fi
    fi
    I know little about sed, but I think is the tool for this job. The most intriging part to me is to ask sed to print the regular expression when it finds it in the config file, and use that as an input in the conditional statement.
    Well, this is a mess I have done here. Hope there is a simple answer to this.
    Thanks in advance.-

    You can do this with a single sed command:
    sed -i 's/^#set proxy_url/set proxy_url/;
    t end;
    s/^set proxy_url/#set proxy_url/;
    : end' config_file
    This edits the file in-place (-i) and first tries to replace the commented with the uncommented line. If that suceeds, sed jumps to the "end" label. If not, it tries to replace the uncommented with the commented line. Thus you don't have to include any logic about the current state: if the first substitution succeeds, the line was obviously commented, if not, it was uncommented, and the second substitution should succeed.
    Note that my knowledge of sed is very limited. There might be a simpler way to do this.
    EDIT: For the sake of example, here's how to do the same in bash using regular expressions. Note how this script needs to use a temporary file to simulate in-place editing, how it needs to process the file line by line manually, etc. All things that sed does out of the box...
    #!/bin/bash
    tmp=test.conf.tmp
    echo -n "" > "$tmp"
    while read line; do
    if [[ "$line" =~ ^#set\ proxy ]]; then
    echo "${line/\#/}" >> "$tmp"
    elif [[ "$line" =~ ^set\ proxy ]]; then
    echo "#$line" >> "$tmp"
    else
    echo "$line" >> "$tmp"
    fi
    done < test.conf
    mv test.conf.tmp test.conf
    To answer your original question, the line
    if [[ "$line" =~ ^#set\ proxy ]]; then
    reads: if the line begins with a "#", followed by "set proxy", then...
    Last edited by hbekel (2011-03-20 10:40:16)

  • How to download file using ftp in bash script

    Hi! I'm runnig a bash script in solaris i want within the script to dowload file using ftp
    How can i do it?
    Tanks a lot

    hello,evgchech
    please try this way:
    1. In the bash script, try following command:
    ftp -n < ftpcmdfile2 in the ftpcmdfile (which is a file),coding the interactive commands of FTP such as:
    user anonymous  [email protected]
              cd /var/sun/download
              bi
              mget *.*
              bye
         try it and good luck!
    Wang Yu
    Developer Technical Support
    Sun Microsystems
    http://sun.com/developers/support

  • Dowload file using ftp in bash script

    Hi! I'm runnig a bash script in solaris i want within the script to dowload file using ftp
    How can i do it?
    Tanks a lot

    hello,evgchech
    please try this way:
    1. In the bash script, try following command:
    ftp -n < ftpcmdfile2 in the ftpcmdfile (which is a file),coding the interactive commands of FTP such as:
    user anonymous  [email protected]
              cd /var/sun/download
              bi
              mget *.*
              bye
         try it and good luck!
    Wang Yu
    Developer Technical Support
    Sun Microsystems
    http://sun.com/developers/support

  • A bash script to backup system only with modified files

    Hi,
    I've made a simple bash script to exam which files are modified and needed to be backed up.
    It will use:
    1. the hash in Pacman's database (/var/lib/pacman/local/<pkg_ver>/files
    2. if no hash in the database, calculate it by our self from cache (/var/cache/pacman/pkg/<pkg_ver>.pkg.tar.gz
    3. if no cache found, compare build date and last modified date
    4. otherwise, this file better be backed up.
    Currently it will only print which files are needed to be backed up, but no backup actually happens. (dry run)
    And it is only in early development stage, please be careful.
    USAGE:
    <the script name> <where to backup files, a directory> <the files, or directories, separated by space, that are going to be examined>...
    Here is the code.
    #!/bin/bash
    # usage:
    # $1: file to backup
    function do_backup() {
    echo
    function smart_bak() {
    pkg=$(pacman -Qo "$1" 2>/dev/null)
    if [ 1 -eq $? ] ; then
    # No package owns $1 (locally created)
    if [ "$1" != "${1%.pacsave}" ] ; then
    echo "skip $1 (left by removed package)"
    else
    echo "backup $1 (local file)"
    fi
    else
    pkg=${pkg#$1 is owned by }
    # evaluate
    # by hash
    cur=$(md5sum $1)
    cur=${cur%% *}
    pkg_ver=${pkg/ /-}
    org=$(grep "^${1:1}" "/var/lib/pacman/local/$pkg_ver/files")
    org_hash=${org##* }
    if [ "$org" != "$org_hash" ] ; then
    # the org hash is in Pacman's database
    if [ "$org_hash" = "$cur" ] ; then
    echo "skip $1 (original config)"
    else
    echo "backup $1 (modified config)"
    fi
    else
    # no hash
    # find out hash myself?
    ARCH=$(uname -m)
    if [ -r "/var/cache/pacman/pkg/$pkg_ver-$ARCH.pkg.tar.gz" ] ; then
    org=$(tar -Oxzf "/var/cache/pacman/pkg/$pkg_ver-$ARCH.pkg.tar.gz" "${1:1}" | md5sum -)
    org_hash=${org%% *}
    if [ "$cur" = "$org_hash" ] ; then
    echo "skip $1 (original)"
    else
    echo "backup $1 (modified)"
    fi
    else
    # no cache, may be a AUR package
    # fall back to built date?
    date=$(pacman -Qi ${pkg% *} | grep "^Build Date")
    date=${date#*: }
    date=$(date -d "$date" +%s)
    mod=$(ls -l $1)
    mod=${mod% $1}
    mod=${mod#* * * * * }
    mod=$(date -d "$mod" +%s)
    tmp1=$(expr $mod "+" 60)
    tmp2=$(expr $mod "-" 60)
    if [ "$date" -le "$tmp1" -a "$date" -ge "$tmp2" ] ; then
    echo "skip $1 (the same date)"
    else
    echo "backup $1 (unknown)"
    fi
    fi
    fi
    fi
    function smart_bak_dir() {
    for i in $(ls -A "$1") ; do
    tmp="${1%/}/$i"
    if [ -f "$tmp" ] ; then
    smart_bak "$tmp"
    elif [ -d "$tmp" ] ; then
    smart_bak_dir "$tmp"
    else
    echo "skip $tmp (not a regular file nor a directory)"
    fi
    done
    # usage:
    # $1: the directory to store this backup
    # $2: directory to evalualte for backup
    # function main()
    # init
    target="$1"
    shift
    # check
    if [ ! -d "$target" -o ! -x "$target" -o ! -w "$target" ] ; then
    exit 4
    fi
    for i in $* ; do
    if [ -f "$i" ] ; then
    smart_bak "$i"
    elif [ -d "$i" ] ; then
    smart_bak_dir "$i"
    else
    echo "skip $i (unknown argument)"
    fi
    done
    Good luck,
    bsdson
    Last edited by bsdson.tw (2008-12-30 07:53:05)

    Thanks for this script. Nice work.
    Can we traverse the pacman.log backwards up and rollback each operation (including "installed" in this). Something like:
    history=$(tail -n +$(grep -m 1 -n "$1" "$LOG" | cut -d ":" -f 1) "$LOG" | tac | grep -E "(upgraded|installed)" | cut -d " " -f 3-)
    Last edited by g33k (2008-11-04 15:08:07)

  • Bash script to dumpstream many files simultaneously with mplayer

    hi guys
    i have a problem which i´m sure can be solved with the power of bash scripting
    unfortunately i´m no bash scripting guru and all my experiments failed so far
    the problem:
    i have a file in which are links(streaminglinks)
    mplayer offers the funtion to dump such a stream with simply issuing
    mplayer -dumpstream mms://path/to/video -dumpfile video1
    for example.
    now i want mplayer to download this streams specified in the links-file automatically.
    basically all it required is a bash script which goes through the link file and generates a command like mplay -dumpstream <link> -dumpfile video<n>
    (where n is the nth link) and execute it.maybe there a even simpler solutions
    well since i´m not that experienced with bashscripting i can´t solve that problem at my self....
    i´m grateful for any help

    hey guys
    thx for the two scripts.
    my approach was nearly the same as your´s kraluz with the difference that it doesn´t work
    but they both have a little blemish
    they download the files sequentially not simultaneously
    how could that be realised
    thx in advance

  • Simple BASH script to update subversion files

    This is just a simple BASH script that will update all .svn files in a specified directory.  If an update fails, it will attempt to update all the subdirectories in the failed one, so as much will be updated as possible.  Theoretically, you should be able to supply this script with only your root directory ( / ), and all the .svn files on your computer will be updated.
    #! /bin/bash
    # Contributor: Dylon Edwards <[email protected]>
    # ================================
    # svnup: Updates subversion files.
    # ================================
    #  If the user supplies no arguments
    #+ then, update the current directory
    #+ else, update each of those specified
    [[ $# == 0 ]] \
        && dirs=($PWD) \
        || dirs=($@)
    # Update the target directories
    for target in ${dirs[@]}; do
        # If the target file contains a .svn file
        if [[ -d $target/.svn ]]; then
            # Update the target
            svn up $target || {
                # If the update fails, update each of its subdirectories
                for subdir in $( ls $target ); do
                    [[ -d $target/$subdir ]] &&
                        ( svnup $target/$subdir )
                done
        # If the target file doesn't contain a .svn file
        else
            # Update each of its subdirectories
            for subdir in $( ls $target ); do
                [[ -d $target/$subdir ]] &&
                    ( svnup $target/$subdir )
            done;
        fi
    done

    Cerebral wrote:
    To filter out blank lines, you could just modify the awk command:
    ${exec awk '!/^$/ { print "-", $_ }' stuffigottado.txt}
    very nice; awk and grep: two commands that never cease to amaze me.

  • Need a perl script which monitors a network folder and sends email when new files/folders arrived...

    I need a perl script (or something else better) which monitors a network folder and sends email when new files/folders arrived. I tried it in Automator but failed.
    Thanks!

    Yes. I tried it. But whenever I restart m Mac, the watch folder doesn't work until I reconnect to network or run the Automator... I need a simple system that automatically connects to the network and monitor the folder, even I restart the Mac.
    Thanks!

  • Simple bash script in SL to remove files

    1. SL on MBP
    2. launch Terminal
    3. pico remove-files
    4. in my script are two simple rm /path to file type statements
    5. save file in user/bin directory
    6. type name of command
    7. get "command not found" error
    no wonder 99.9999% of the world doesnt program - why doesnt this simple setup work right ?

    Chris Chamberlain1 wrote:
    just guessing, if it type ./remove-files this script works.
    That's the traditional way to invoke a program or command file if its location is not in your path. If that file is in /usr/bin and /usr/bin is in your path, I'm not sure why your situation doesn't work. On my Mac the directory bin in /usr is owned by root:wheel. If yours is the same, how did you manage to create your script there without permission manipulations? What exactly did you mean when you first mentioned "save file in user/bin directory"? Did you really mean /usr/bin or something else? If you meant some other location, you might want to add that location to your path with this command:
    PATH=yourotherdirectory:$PATH

  • Bash script to pseudo multithread a encode job lame

    Conceptually, I don't see why a bash script couldn't run x simultaneous lame mp3 encodes where x is the number of cores the user selects.  I just don't know how to go about doing it.  Would it make sense to read in all the .wav files to an array, then divide the array into x sub arrays and feed the work out or is there a more simplistic method?
    EDIT: I found this blog entry but I don't fully understand it. 
    Here is my adaptation of his code:
    #!/bin/bash
    # the number of cores you have on your system
    cores=4
    encode() {
    for file; do
    fileout=$(echo "$file" | sed s'/.wav//')
    lame -V 2 "$file" "$fileout".mp3; done
    export -f encode
    find . -name '*.wav' -print0 | xargs -0 -n 1 -P $cores bash -c 'encode "$@"' --
    Can someone explain to me what the last bit does: 'encode"$@"' --
    Last edited by graysky (2010-03-01 22:17:13)

    The last bit calls the encoide function, passing in all the arguments as past to it on stdin.
    'encode "$@"' calls encode and passes it its arguments (as passed to it from the find command).
    -- tells xargs that there are no more command line options, and to interpret anything "-" as part of the file names.
    man find and man xargs for more info.
    The "--" I'm not sure which man page it'll be in but I think it's a GNU thing?

  • Bash script to sync KeepassX database

    I created a my first little bash script to sync my Keepass(X) (Keepass on Windows boxes and KeepassX on Linux boxes) over a secure (SSL + Login) webdav enabled site.
    #!/bin/bash
    host=https://www.some_dav_enabled_website.com/db.kdb
    user=foo
    pass=bar
    fpath=/somefolder/db.kdb
    function update {
    echo "Deleting old KeepassX database..."
    rm $fpath
    echo "Downloading new KeepassX database..."
    curl -sku $user:$pass $host -o $fpath
    echo "Starting KeepassX..."
    keepassx $fpath &
    function sync {
    echo "Uploading the KeepassX database to server..."
    curl -sku $user:$pass -X PUT -T $fpath $host &> /dev/null
    echo "Database updated!"
    while getopts "us" optname
    do
    case "$optname" in
    "u")
    update
    "s")
    sync
    echo "Usage: -u Update to the latest KeepassX database."
    echo " -s Sync the (un)modified KeepassX database."
    esac
    done
    Enjoy !
    Last edited by SiB (2008-07-31 23:58:59)

    Hi Sybrand Bakker,
    I tried streams for the replication purpose as per your suggesion, till Iam unable make it work , i dont find a step by step document which will make it possible without error.
    One more thing , i need the streams work without Database link, ie., source database is not connected directly to destination database. I need to create streams and transfer the stream as a file through FTP.I need to download stream file in remote location and then apply the streams to destination database and after this source and destination database should be same in data and schema.
    Please suggest me a solution to go abt this scenario. We are in critical stage to make it happen...
    thanking you in advance
    with regards
    vivek
    Message was edited by:
    Vivekanandh

  • Questions concerning basic bash scripting

    Good evening,
    at the moment we do bash scripting at university, but I seen to encounter some problems in understanding the functionality of bash. I would be very grateful if you guys could help me out with one or another question.
    First one:
    Task to accomplish: Scripts with three arguments. $1 -> source directory, $2 -> file suffix, $3 -> target directory. Search source directory for all files with suffix $2 and copy them to $3.
    I tried to do it, using pipe and xargs, but I am wondering how to pass the filenames found over the pipe.
    find $source -name "*.$suffix" | xargs cp [MISSING ARGUMENT] $target
    How do I access the data I need to fill in as first argument of cp, or isn't it possible to accomplish the task this way?
    Regards,
    cg

    chaosgeisterchen wrote:find $source -name "*.$suffix" | xargs cp [MISSING ARGUMENT] $target
    Almost, you just need the -t flag to cp which basically reverses the order of SOURCE and TARGET in this instance
    find $source -name "*.$suffix" | xargs cp -t $target
    Also, I suggest you escape the * in your find to prevent bash expanding it. Bash shouldn't expand, find should.
    You might like to also use the -iname (as opposed to -name) predicate to find if you don't care about the case of the source files.
    Last edited by fukawi2 (2008-06-02 03:13:37)

  • [solved]Need help with a bash script for MOC conky artwork.

    I need some help with a bash script for displaying artwork from MOC.
    Music folders have a file called 'front.jpg' in them, so I need to pull the current directory from MOCP and then display the 'front.jpg' file in conky.
    mocp -Q %file
    gives me the current file playing, but I need the directory (perhaps some way to use only everything after the last  '/'?)
    A point in the right direction would be appreciated.
    thanks, d
    Last edited by dgz (2013-08-29 21:24:28)

    Xyne wrote:
    You should also quote the variables and output in double quotes to make the code robust, e.g.
    filename="$(mocp -Q %file)"
    dirname="${filename%/*}"
    cp "$dirname"/front.jpg ~/backup/art.jpg
    Without the quotes, whitespace will break the code. Even if you don't expect whitespace in any of the paths, it's still good coding practice to include the quotes imo.
    thanks for the tip.
    here it is, anyhow:
    #!/bin/bash
    filename=$(mocp -Q %file)
    dirname=${filename%/*}
    cp ${dirname}/front.jpg ~/backup/art.jpg
    then in conky:
    $alignr${execi 30 ~/bin/artc}${image ~/backup/art.jpg -s 100x100 -p -3,60}
    thanks for the help.
    Last edited by dgz (2013-08-29 21:26:32)

  • Bash script help?

    I really need to get a good book to read and learn some bash scripting so I can do this myself. but this is only the 3rd or 4th time i've come here asking
    I was hoping someone might be able to assist me again
    I'm trying to organize my movie folder again. I've let it go too long
    basically, i'm looking for a bash script to go through my folder of movies. each movie has it's own folder with an avi, jpg and nfo. I need the files in the folders to be the same name as the parent folder
    so lets say i have this
    movie title folder (2012)
    -movie_file_2012.avi
    -an_nfo_file.nfo
    -a_jpg.jpg
    I want to turn into this
    movie title folder (2012)
    -movie title folder (2012).avi
    -movie title folder (2012).nfo
    -movie title folder (2012).jpg
    anyone?

    ssl6 wrote:
    I really need to get a good book to read and learn some bash scripting so I can do this myself. but this is only the 3rd or 4th time i've come here asking
    I was hoping someone might be able to assist me again
    I'm trying to organize my movie folder again. I've let it go too long
    basically, i'm looking for a bash script to go through my folder of movies. each movie has it's own folder with an avi, jpg and nfo. I need the files in the folders to be the same name as the parent folder
    so lets say i have this
    movie title folder (2012)
    -movie_file_2012.avi
    -an_nfo_file.nfo
    -a_jpg.jpg
    I want to turn into this
    movie title folder (2012)
    -movie title folder (2012).avi
    -movie title folder (2012).nfo
    -movie title folder (2012).jpg
    anyone?
    I won't do the work for you, but if you're willing to look things up and try to write a working script that does what you've described--I'll be happy to help flush out problem areas.
    Okay, so what you want is to turn
    Movies/:
    A-Movie-I-love.avi
    A-Movie-I-love.nfo
    A-Movie-I-love.jpg
    A-Movie-about-Llamas.avi # :P
    A-Movie-about-Llamas.nfo
    A-Movie-about-Llamas.jpg
    into this:
    Movies/:
    A-Movie-I-love/:
    A-Movie-I-love.avi
    A-Movie-I-love.nfo
    A-Movie-I-love.jpg
    A-Movie-about-Llamas/:
    A-Movie-about-Llamas.avi
    A-Movie-about-Llamas.nfo
    A-Movie-about-Llamas.jpg
    right?
    Last edited by lspci (2013-04-23 20:40:30)

Maybe you are looking for

  • Using Mini DisplayPort to VGA, screen resolution distorts.  How do I fix?

    When I use my Mini DisplayPort to VGA adapter, the resolution gets too big for my screen (mbpro 15") and the same thing appears on the projector I use. I'm not sure if I should change my screen resolution while I use the projector or what? Also, the

  • Looking for a Template

    New Member Posts:  1 Location:  Ames, IA Joined:  11th Dec 2009 PM I'm new to DreamWeaver, but more significant is that this is my first attempt of building a website from scratch. In the past I have used what I would discribe as "Cookie Cutter" appl

  • What's the difference between data source and source system in BI

    hello what's the basic and exact difference in BW 3.0 and BI 7.0. i am very urgent indeed. can anyone help me - how the things should be done here. it's preety confusing. Can anybody give me the step by step analysis. Thanks in advance C.S.Ramesh

  • Ibm MP4, divx, jmf 2.1.1e

    Hello. I've tested the above configuration to play a standard divx. i had problems, of course, and nothing finally works. Using jmf studio on XP, i get a pink background, and finally the program refuses to exit. In software mode (no acceleration sele

  • [solved] udev fails to register anything on boot after kmod

    Hello, After yesterday's kmod upgrade, i can't seem to get udev to modprobe anything on boot. Udev throws about two dozen errors during boot complaining about "no such file or directory", but /sbin/modprobe exists, and seems to work fine with udev af