![]() |
Shell Scripting help
What I want to do is to index certain folders to a text file. I by using the ls-alR > my_file will give me a text file what is in a certain folder and go down recursively.
What I want to do is to script this with an applescript/shell script; since I do this so much. I can get it to work just by doing the ls command, but I want to script the command and have it name it to the folder that I am indexing. I have no idea how to do that. |
So... you just want to take the command and put it into a script so you can double click it?
Shell Scripting 101: http://osxfaq.com/Tutorials/Learning...ting1/index.ws |
more that that...that much I know. I want it to take the name of the folder I am indexing.
such as: Folder of Files Then have it run the ls command I spoke of and name it folder of files.txt I wanted to do this with a folder actions. Drop folder(s) onto folder...then have it index and name the text files according to the folder name. |
hmm you could try something like this
Code:
#!/bin/bashthen to invoke it via apple script Code:
tell application "terminal.app" |
what I really need help on is setting to name of the folder to a variable to place into the script...so its more like
ls -alR $filename > $filename.txt for some reason I get it to do everything, but make the variable take on that filename. Especially if it has spaces in it. |
okay...so I have it sort of working now. But when I run the ls command in terminal I get this:
ls: 831/: No such file or directory ls: Archive: No such file or directory ls: CD: No such file or directory |
Put quotes around the variable, and some braces? Otherwise, I'm not sure how bash parses the second one.
ls -alR "$filename" > "${filename}.txt" |
I did that and I got:
./index.sh: line 6: {Foldername/}.txt: No such file or directory |
You do have to avoid having the / on the end of the folder name, since that's not legal in a filename
You've got something else going on with the brackets, too. Post the script as you have it now? |
Quote:
# Tony |
I'm not a Unix expert, but I think the problem here is that many Unix commands don't deal well with variables. They're fine if you want to define a variable, but not so good at using them, especially with long paths. Here's an example that works except for two lines:
Code:
#!/bin/sh~/Desktop/my_file5.txt cat $d returns: cat: ~/Desktop/my_file5.txt: No such file or directory and although the line above it correctly finds its file, this one doesn't ls -alR $c > $d it produces this error: ~/Desktop/my_file5.txt: No such file or directory And yes, my_file5.txt exists right next to 1 through 4. |
Quote:
Double quotes only allow for variable expansion. They don''t allow tilde or brace expansion. Some quick experiments with echo will show this. Code:
echo $0# Tony |
hmm I am learning a few new tricks in shell scripting in this thread.
I have used varibles before in a sense that 1=x 2=y 3=z so and and so forth and then the script calls for an option and it you input option 1, then it runs the set of commands that equal x. |
Honestpuck,
Thanks! That's been bothering for a while, and none of the tutorials talk about it. At least none that I've read. :) |
Oh, and in reply to the question of how to get just the filename portion of
the a path - use awk. awk -F/ '{ print $(NF) }' The above will work if you don't have a '/' at the end. If you do then replace (NF) with (NF - 1) (Notice that we use single quotes around the awk command so the shell doesn't try and expand the variable reference, which is meant for awk in this case NOT the shell.) # Tony |
Quote:
# Tony |
Quote:
Code:
ls -alR "$filename" > "`echo $filename | awk -F/ '{ print $(NF) }'`.txt"# Tony |
Since I learned something here I thought that I should provide something useful. Here's an Applescript that works on the front most Finder window. It assumes that the file: filename.txt is in the folder you're scanning, but you can change that easily enough if you care to.
Code:
tell application "Finder" |
Addin that awk on the end totally did exactly what I wanted it to do!!! Thank you so much for the help...THANK YOU THANK YOU THANK YOU!!!
|
Okay...so how do I make this a folder action so I can drop the folder I want indexed into another one. I tried doing this:
on adding folder items to this_folder after receiving these_items tell application "Terminal" activate do shell script "/Users/me/Temp/To Be Burned/Indexed/index.sh" end tell end adding folder items to |
I think it would be easier to run the script with the folder opened and use the "set source_folder" line from post #18
If you drop the folder, it's location will change. |
The "canonical" way to get just the last part of a file (or folder) name is to use "basename":
Code:
~ % basename /Users/ataraxia/Documents/resume.rtfCode:
~ % basename /Users/ataraxia/Documents/resume.rtf .rtfCode:
~ % dirname /Users/ataraxia/Documents/resume.rtf |
Quote:
command for that (btw, why would a file's name end in /?): basename "$a" -HI- |
Something weird is going on here:
When I wrote and submitted my above post at 4:14 PM [if indeed it was 4:14 PM?], ataraxia's reply (designated 2:46 PM?) was not here!!! In fact... my reply originally landed on page 1. Strange. |
| All times are GMT -5. The time now is 05:41 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.