Solved

Calling a command recursively under subdirectories in TCSH

Posted on 2004-04-08
3
312 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now