Solved

Calling a command recursively under subdirectories in TCSH

Posted on 2004-04-08
3
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

717 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