C-shell Script to Bash Script?

Discussion in 'Apple' started by qquito, Aug 1, 2007.

  1. qquito

    qquito Guest

    Dear Everyone:

    I used to use the following three lines of C-shell script

    foreach file (`ls -1 name.list.txt`)
    a.out $file
    end

    to do a loop. The 1st line lists the content in the file named
    "name.list.txt", and in the 2nd line, an excutable file, a.out, takes
    each listed item as an argument, and the 3rd line ends the loop.

    How would you convert the above three lines to BAsh (Bourne Again
    shell) script to be used on a Mac?

    Thank you for reading and replying!

    --Roland
     
    qquito, Aug 1, 2007
    #1
    1. Advertisements

  2. Where does it list the contents of name.list.txt? It just lists
    the file (and ls is unnecessary)
    To do the same as that script:

    for file in name.list.txt
    do
    a.out
    done

    If you want to read the contents of name.list.txt, use:

    while IFS= read -r file
    do
    a.out "$file"
    done < name.list.txt
     
    Chris F.A. Johnson, Aug 1, 2007
    #2
    1. Advertisements

  3. qquito

    qquito Guest

    Chris: Just tried what you suggested, and it works. Thanks a lot. ---
    Roland
     
    qquito, Aug 1, 2007
    #3
  4. qquito

    Dave Seaman Guest

    By the way, csh is on the Mac. If you have a csh script that actually
    works, you don't need to convert anything. All you need is an
    appropriate shebang line:

    #!/bin/csh
    foreach file (whatever you actually want here)
    a.out "$file"
    end
     
    Dave Seaman, Aug 1, 2007
    #4
  5. qquito

    John Taylor Guest

    That depends what locale your keyboard is.

    On my keyboard ~ is on the same key as # down near the enter key
    back-tick is with ¬ and ¦ top left, next to 1

    Or do Mac keyboards not have a locale ?

    Regards
    JohnT
     
    John Taylor, Aug 2, 2007
    #5
  6. A literal translation of what you have is

    for file in `ls -l name.list.txt` ; do a.out $file ; done

    What you seem to have in mind:

    for name in `cat name.list.txt` ; do a.out $name ; done
     
    William Mitchell, Aug 2, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.