Simple question about shell scripting on SuSE 8.0
Hallo, I m a shell scripting newbie, and i m trying to create a shell script that search for some files named 'script', those files are located in the subdirectory of a directory named "folder", and for each file founded, i have to show the directory (full path) where the file was founded and pipe the file to some program. the script must be something like that: for each file found in the subdirectory tree of "folder" do "show the full path where the file was found, without the file name, like /root/folder/images" enter this directory already showed ($cd directory) "pipe the file to a program" done Thanks for help!! _________________________________________________________________ Scan and help eliminate destructive viruses from your inbound and outbound e-mail and attachments. http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=htt... Start enjoying all the benefits of MSN® Premium right now and get the first two months FREE*.
On Thursday 05 May 2005 2:37 am, robert guru wrote:
Hallo, I m a shell scripting newbie, and i m trying to create a shell script that search for some files named 'script', those files are located in the subdirectory of a directory named "folder", and for each file founded, i have to show the directory (full path) where the file was founded and pipe the file to some program. the script must be something like that:
for each file found in the subdirectory tree of "folder" do "show the full path where the file was found, without the file name, like /root/folder/images" enter this directory already showed ($cd directory) "pipe the file to a program" done A patrick mentioned, find should work, but here is a bash script that does what you want. The script is not very elegant as I threw it together. In this case, sname is a variable containing the name of a file that has "script" in the name. #!/bin/bash for i in $(ls -p) do case $i in */) cd $i; pth=$PWD; for sname in $(ls) do case $sname in *script*) echo "$sname found in $pth";; esac done cd ..;; esac done
-- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
Hi Peeps, Can I please just pick up on this thread.... If the filename has spaces in it (eg: "Full\ Length\ Movies") how can I get the entire filename into $i because what is happening is, on first iteration $i is "Full" , on 2nd it is "Length" etc... TIA, Zoltan Jerry Feldman wrote:
On Thursday 05 May 2005 2:37 am, robert guru wrote:
Hallo, I m a shell scripting newbie, and i m trying to create a shell script that search for some files named 'script', those files are located in the subdirectory of a directory named "folder", and for each file founded, i have to show the directory (full path) where the file was founded and pipe the file to some program. the script must be something like that:
for each file found in the subdirectory tree of "folder" do "show the full path where the file was found, without the file name, like /root/folder/images" enter this directory already showed ($cd directory) "pipe the file to a program" done
A patrick mentioned, find should work, but here is a bash script that does what you want. The script is not very elegant as I threw it together. In this case, sname is a variable containing the name of a file that has "script" in the name. #!/bin/bash for i in $(ls -p) do case $i in */) cd $i; pth=$PWD; for sname in $(ls) do case $sname in *script*) echo "$sname found in $pth";; esac done cd ..;; esac done
-- ================================== Geograph (Pty) Ltd P.O. Box 31255 Tokai 7966 Tel: +27-21-7018492 Mobile: +27-83-6004028 ==================================
Jerry, On Thursday 05 May 2005 05:30, Jerry Feldman wrote:
#!/bin/bash for i in $(ls -p) do case $i in */) cd $i; pth=$PWD; for sname in $(ls) do case $sname in *script*) echo "$sname found in $pth";; esac done cd ..;; esac done
Two (and a half) things: 1) Quote all references to the variables $i, $pth, $PWD and $sname. 2) Add this line before the for loop: IFS=$'\n' The half thing is to use better (more connotative and less abbreviated) variable names, not to smash so much stuff onto single lines and to avail yourself of the full range of BASH facilities. Here's the script I used to verify my solution to your question: -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- #!/bin/bash IFS=$'\n' for dirEnt in $(ls -p) do case $dirEnt in */) cd "$dirEnt" dir="$PWD" for sName in $(ls); do if [[ "$sName" = *script* ]]; then echo "script \"$sName\" found in $dir" fi # case "$sName" in # *script*) # echo "script \"$sName\" found in $dir" # ;; # esac done cd .. ;; *) echo "Non-dir: \"$dirEnt\"" ;; esac done -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- Lastly, I'll point out that using the "-p" option of "ls" to distinguish the kind of file system entity you're dealing with is probably not the best choice. You're only getting away with it in this script because in most contexts (beware rsync) a directory name means / refers to is the same thing with or without the trailing slash. Other things (the @ sign used to signify a symbolic link or the * used to mark executable files, e.g.) will probably impair any attempt to actually reference the file system entity in question. Check out the "test" (a.k.a. "[") shell built-in for the preferred way to make file type- and mode-specific determinations in your scripts ("help test"). Randall Schulz
On Saturday 14 May 2005 08:58, Randall R Schulz wrote:
if [[ "$sName" = *script* ]]; then
Why the double brackets? what does that mean? -- ====================================================== Glenn Holmer (Linux registered user #16682) ====================================================== "Greater coherence cannot be achieved. Not even the Netherlanders have managed this." -Anton Webern ======================================================
Glenn, On Sunday 15 May 2005 09:31, Glenn Holmer wrote:
On Saturday 14 May 2005 08:58, Randall R Schulz wrote:
if [[ "$sName" = *script* ]]; then
Why the double brackets? what does that mean?
It ain't magic, it ain't something I made up and it _ain't_ undocumented (hint, hint): % help [[ [[ ... ]]: [[ expression ]] Returns a status of 0 or 1 depending on the evaluation of the conditional expression EXPRESSION. Expressions are composed of the same primaries used by the `test' builtin, and may be combined using the following operators ( EXPRESSION ) Returns the value of EXPRESSION ! EXPRESSION True if EXPRESSION is false; else false EXPR1 && EXPR2 True if both EXPR1 and EXPR2 are true; else false EXPR1 || EXPR2 True if either EXPR1 or EXPR2 is true; else false When the `==' and `!=' operators are used, the string to the right of the operator is used as a pattern and pattern matching is performed. The && and || operators do not evaluate EXPR2 if EXPR1 is sufficient to determine the expression's value.
Glenn Holmer (Linux registered user #16682)
Randall Schulz
On Sunday 15 May 2005 11:40, Randall R Schulz wrote:
On Sunday 15 May 2005 09:31, Glenn Holmer wrote:
On Saturday 14 May 2005 08:58, Randall R Schulz wrote:
if [[ "$sName" = *script* ]]; then
Why the double brackets? what does that mean?
It ain't magic, it ain't something I made up and it _ain't_ undocumented (hint, hint):
For a more useful discussion, see this thread from the Milwaukee Linux Users Group mailing list: http://www.milwaukeelug.org/pipermail/mlug-list/2005-May/003880.html -- ====================================================== Glenn Holmer (Linux registered user #16682) ====================================================== "Greater coherence cannot be achieved. Not even the Netherlanders have managed this." -Anton Webern ======================================================
Glenn, On Monday 16 May 2005 17:38, Glenn Holmer wrote:
On Sunday 15 May 2005 11:40, Randall R Schulz wrote:
On Sunday 15 May 2005 09:31, Glenn Holmer wrote:
On Saturday 14 May 2005 08:58, Randall R Schulz wrote:
if [[ "$sName" = *script* ]]; then
Why the double brackets? what does that mean?
It ain't magic, it ain't something I made up and it _ain't_ undocumented (hint, hint):
For a more useful discussion, see this thread from the Milwaukee Linux Users Group mailing list:
How's that? You asked (here) what the double-square-bracket conditionals meant. I told all you need to know. Over on the MLUG list you asked for examples. Had you done that here, I would have given them to you.
http://www.milwaukeelug.org/pipermail/mlug-list/2005-May/003880.html
Glenn Holmer (Linux registered user #16682)
Randall Schulz
participants (5)
-
Glenn Holmer
-
Jerry Feldman
-
Randall R Schulz
-
robert guru
-
Zoltan Szecsei