Solved

Calling a command recursively under subdirectories in TCSH

Posted on 2004-04-08
3
318 Views
Last Modified: 2007-02-13
I have a question similar to

http://www.experts-exchange.com/Operating_Systems/Linux/Q_20287361.html

Where I want to have a script that I can run so that I can call any command recursively on subdirectories.

One example is that I can do something like

rec_cmd mycommand -f hello.*

where the rec_cmd will call "mycommand -f hello.*" on all subdirectories. The following solution was in bash. I'm looking for one under tcsh.

#!/bin/bash
find -type d | awk "{print \"export STCD=`pwd` ; cd \" \$0 \" ; $1 ; cd $STCD\"}" | sh -x

Thanks.

-Edward
0
Comment
Question by:edwardt
3 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 250 total points
ID: 10788941
Edward, why do you think you need a version for tcsh? If this script works for you, just use it. Chances are that you have Bash on your system. Tcsh is a nice shell for interactive work, but really has it's problems when used for scripts. You have to use the right tool for the job, and Bash (or sh) is definitely better than tcsh in this case. With tcsh you always have a hard time quoting, and without having tried it, I'm pretty sure that getting this into a tcsh format will result in some horrible combination of single and double quotes.
BTW: Your script may not work with all flavors of find. If you use
find . -type d -print | ...
your chances are much better that it will work on all systems.

Also, you can simplify the script:

#!/bin/bash
find -type d | awk "{print \"( cd \" \$0 \" ; $1 )\"}" | sh -x


0
 
LVL 40

Expert Comment

by:jlevie
ID: 10788955
That script, or similar, will work fine on your Linux system since it runs in a bash (Bourne shell equivalent) subshell. It's the #!/bin/bash at the beginning of the script file that starts a bash shell to execute the contents of the script file. In general people use Bourne shell for scripts since it is the most basic shell and is present on every version of Linux or Unix. That means that any scripts are portable, regardless of what Linux or Unix they are used on or what shell the user might happen to be using.
0
 

Author Comment

by:edwardt
ID: 10921875
i was thinking tcsh because i use tcsh as my shell, and call functions using aliases, so if i did something like

rec_cmd command

that the command is an alias in my ~/.tcshrc

But I guess I can put that alias in my ~/.bashrc and it shoudl work too.

thanks.

-edward
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SonarQube on Linux vs Windows 3 52
ftp to port 21 4 54
LogmeIn using Linux Ubuntu 16.04 6 63
Linux MD5 Hash 7 40
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question