Solved

Calling a command recursively under subdirectories in TCSH

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…
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.

733 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