The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Shell Scripting help (http://hintsforums.macworld.com/showthread.php?t=73372)

playingreprise 06-06-2007 02:57 PM

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.

yellow 06-06-2007 03:14 PM

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

playingreprise 06-06-2007 03:33 PM

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.

tlarkin 06-06-2007 04:24 PM

hmm you could try something like this

Code:

#!/bin/bash

ls -alR /Volumes/Macintosh\ /HD/Documents | open -t -f

save this as a shell script, like index.sh

then to invoke it via apple script

Code:

tell application "terminal.app"

run /path/to/script/index.sh

end tell

its kind of crude but worked for me. You will have to make sure you resave every text file as "save as" text file name.

playingreprise 06-06-2007 04:44 PM

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.

playingreprise 06-06-2007 05:01 PM

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

ataraxia 06-06-2007 05:21 PM

Put quotes around the variable, and some braces? Otherwise, I'm not sure how bash parses the second one.

ls -alR "$filename" > "${filename}.txt"

playingreprise 06-06-2007 05:35 PM

I did that and I got:
./index.sh: line 6: {Foldername/}.txt: No such file or directory

ataraxia 06-06-2007 06:38 PM

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?

honestpuck 06-06-2007 07:38 PM

Quote:

Originally Posted by playingreprise (Post 383976)
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.

You don't make clear if you want one text file to hold the contents of all the directories in the tree or you want one text file for each directory. The answer would be different for each possibility.

# Tony

cwtnospam 06-06-2007 08:55 PM

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
a="/tmp/my.txt
b="/tmp"
echo $b
cd $b
ls -alR /tmp > ~/Desktop/my_file1.txt
ls -alR $b > ~/Desktop/my_file2.txt
ls -alR $b > $a
c="/Users/myusername/Desktop/Addresses"
d="~/Desktop/my_file5.txt"
echo $c
cd $c
cd /Users/myusername/Desktop/Addresses
echo $d
cat $d
ls -alR /Users/myusername/Desktop/Addresses > ~/Desktop/my_file3.txt
ls -alR $c > ~/Desktop/my_file4.txt
ls -alR $c > $d

Although echo $d produces:
~/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.

honestpuck 06-06-2007 09:15 PM

Quote:

Originally Posted by cwtnospam (Post 384053)
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
a="/tmp/my.txt
b="/tmp"
echo $b
cd $b
ls -alR /tmp > ~/Desktop/my_file1.txt
ls -alR $b > ~/Desktop/my_file2.txt
ls -alR $b > $a
c="/Users/myusername/Desktop/Addresses"
d="~/Desktop/my_file5.txt"
echo $c
cd $c
cd /Users/myusername/Desktop/Addresses
echo $d
cat $d
ls -alR /Users/myusername/Desktop/Addresses > ~/Desktop/my_file3.txt
ls -alR $c > ~/Desktop/my_file4.txt
ls -alR $c > $d

Although echo $d produces:
~/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.

That's because by using "~" you are relying on the shell's tilde expansion not a variable and then surrounding it in double quotes protects it from shell expansion and passes the string intact. Commands don't understand tilde, only the shell. The "echo" output was the giveaway clue.

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
echo "$0"
echo '$0'
echo ~
echo "~"
echo a{d,c,b}e
echo "a{d,c,b}e"

The easy work around in this case is to replace '~' with '$HOME'.

# Tony

tlarkin 06-06-2007 09:29 PM

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.

cwtnospam 06-06-2007 09:31 PM

Honestpuck,
Thanks! That's been bothering for a while, and none of the tutorials talk about it. At least none that I've read.
:)

honestpuck 06-06-2007 09:53 PM

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

honestpuck 06-06-2007 09:56 PM

Quote:

Originally Posted by cwtnospam (Post 384060)
Honestpuck,
Thanks! That's been bothering for a while, and none of the tutorials talk about it. At least none that I've read.
:)

Twenty years of fighting the shell, cwt. You get to recognise the signs in the chicken entrails :)

# Tony

honestpuck 06-06-2007 10:12 PM

Quote:

Originally Posted by playingreprise (Post 384001)
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.

Code:

ls -alR "$filename" > "`echo $filename | awk -F/ '{ print $(NF) }'`.txt"
Take real care with quotes, backticks and double quotes in that one.

# Tony

cwtnospam 06-06-2007 10:27 PM

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"
        activate
        set the source_folder to (folder of the front window) as alias
        set fldr to POSIX path of source_folder
        set flname to fldr & "filename.txt"
end tell
tell application "Terminal"
        do script "ls -laR " & fldr & " > " & flname
end tell

You need admin rights & sudo for system folders, etc.

playingreprise 06-07-2007 11:05 AM

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!!!

playingreprise 06-07-2007 11:41 AM

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

cwtnospam 06-07-2007 12:05 PM

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.

ataraxia 06-07-2007 02:46 PM

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.rtf
resume.rtf

You can also get it to remove a suffix for you:
Code:

~ % basename /Users/ataraxia/Documents/resume.rtf .rtf
resume

"dirname" does the opposite:
Code:

~ % dirname /Users/ataraxia/Documents/resume.rtf
/Users/ataraxia/Documents


Hal Itosis 06-07-2007 04:14 PM

Quote:

Originally Posted by honestpuck (Post 384064)
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)

If I followed the above discussion correctly, there's also a specific
command for that (btw, why would a file's name end in /?):

basename "$a"

-HI-

Hal Itosis 06-08-2007 10:51 AM

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.