I need help for a terminal script

Discussion in 'Mac Programming' started by Dfanoe, Jan 24, 2018.

  1. Dfanoe macrumors newbie


    Jan 24, 2018
    I really need help... I am NEW to this scripting, using terminal and whatnot, so please be patient :).

    I have a list of filenames (currently with no file extension) in an Excel file and I need to copy those specific files from one folder to another.

    I tried using this script in Terminal:


    fnames=$(cat /Users/dfanoe/Desktop/STEPpics/Final.xlsx)

    for i in $fnames; do
    cp ${target}/${i} ${destination}/
    echo "copying $i"
    echo "done"

    I found it here on the forum but I cant get it to work.

    Should the file names in the Excel file all have extension, ie .jpg?

    The target folders name is "pic" and the destination folders name is "Found" but when I copy it into Terminal it says that the folder Found couldnt be found? The excel file name is "Final.xlsx"

    Is there any way you guys can help me? Again - please be very specific since this is practically foreign language to me...
  2. kryten2 macrumors 6502a

    Mar 17, 2012

    Where did you find the script? Who's dina? I doubt the cat command can do anything with an Excel xlsx file. I downloaded a sample.xlsx file to test with LibreOffice Calc and cat in Terminal.

    Attached Files:

  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    This is a general shell script question. There's nothing really specific to Macintosh. You could run this script on Linux, for instance.

    AFAIK, shell scripts don't have any kind of debugging support. When writing shell scripts the echo command is your friend. Use it to print out where you are in the script and the values of your variables. There are a couple examples of this in your script but you probably need more.

    echo "step one"
    echo $target
    # etc.

    You can learn about shell commands using the man command.

    man cat
    man cp

    Most likely paths in your script need to be full paths. That is they need to start with /. Also, all paths will be case sensitive. You may need to convert your excel file to a csv file to work with it in the terminal.
  4. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK

    I wouldn't expect any meaningful output from reading an Excel file with "cat". If possible, export it as a CSV file - which is, in effect, plain text - and take it from there.
  5. Dfanoe thread starter macrumors newbie


    Jan 24, 2018
    Okay - see I have no idea what the "cat" stands for :). I changed the excel file to a csv file but it still says the destination-folder does not exist??

    Attached Files:

  6. superscape, Jan 29, 2018
    Last edited: Feb 7, 2018

    superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK
    Ah. Okay. Well, there's one very obvious problem here! ;-)

    You've just pasted the script into a Terminal window, and that's not how you run a shell script. You can do that with simple commands, sure, but there are returns etc in your script that are messing things up here.

    I'd recommend you paste the script into a text file. NB. A text file isn't the same thing as a Word file. Word processors insert all sorts of rubbish into your file that will mess your shell script up. I like BBEdit - you can download it and use it free for a while. https://www.barebones.com/products/bbedit/ Other text editors are available.

    Next, make a new text document in BBEdit, paste your script into it and save it with a ".sh" extension. See my screen shot "bbedit.png". Save it somewhere - lets say your Desktop.

    Next, launch Terminal and type "sh ", but without the quotes. Don't forget the space. Next drag your script into your Terminal window. It should automatically work out the script's path for you, adding any appropriate escaping. If it looks a bit odd, don't worry. It should look something like my screen shot "terminal.png"

    Next, hit "return" to run the script.

    I expect you will still have problems with file paths. If you're not sure what they are, you can use the trick above (drag them into your Terminal window) to figure out what they should be. Then copy and paste them into your script.

    As a side note, since you didn't know what "cat" does. In the Terminal you can type:

    man commandname

    ...replace commandname with the command, e.g. cat.

    Hope that's some help!

    Attached Files:

  7. cqexbesd macrumors regular

    Jun 4, 2009
    If this is a one off its probably simpler to just copy and paste the list out into a text file. If this needs to happen often enough that that is a pain then there are ways around it.

    Those path names have to be valid. They are relative patches (i.e. taken from which directory you are in when you run the script) but my guess would be that they may be wrong.

    In the same directory as you run the script you can try
    ls path
    and if path is wrong you will get an error (No such file or directory). Just guessing but maybe you want
    in front of each path?

    This will only work with plain text files, hence the suggestion to copy and paste out of excel above. If you really need to take data directly from an excel spreadsheet then this command will need to be more complicated.

    You probably want to use quotes in there, else it will break if file names have e.g. spaces in them. The final / is superfluous in this case (but doesn't hurt either).

    cp "${target}/${I}" "${destination}"

    When you run the script you can do so using
    bash -x name_of_script
    (where name_of_script is a plain text file in which you have saved the script). This will print out each command as it is run and may help you see what is producing errors or which variables have expanded to something you are not expecting.
  8. DennisBlah macrumors 6502


    Dec 5, 2013
    The Netherlands
    With such one time things I usually just generate a list of commands using excel.

    Lets say column A contains filenames only (no extention)
    I set a formula in column B

    =“cp “ & A1 & “* /path/of/target/folder/“
    *You can rework the formula by checking if A1 is not empty etc etc, but yeah one of time stuff is usually quick and dirty to me.

    Drag the formula over all rows
    Select all the lines from column B and copy to clipboard
    Open terminal and browse to the folder of the files, and just simply paste

    Please note that the cp command assumes the file is in the folder where it’s running from.
    As you said without extention, I added a * so it would copy ‘any’ file which starts with the value of column A

    To get back on the script, indeed it has to be saved as a file and ran like others mentioned already and do lack the escaping of spaces thus shud the filesname and path to new folder be enclosed within “ at all times.

    Also I doubt that terminal can get the output of a excel file... I think you interpreted it wrong, try to copy your excel rows to a txt or save the excel ad csv file and use that one in the script, that will get you further down the script and issues

    If I have time I sometimes like to use applescript instead, as that can work with excel or almost any other application

Share This Page

7 January 24, 2018