LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2005, 02:47 AM   #1
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Rep: Reputation: 15
Why can't I use bash to do this ?


I edit a scrit, and it has two word like these :
Code:
for temp in 'ls -d */'
do
cd $temp
done
But ,when I run it ,it always divided the for code like :
Code:
cd ls
cd -d
cd */
Why ?
 
Old 10-30-2005, 03:00 AM   #2
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Because you're using the wrong quotation mark. What you're looking for is the backtick: ` not the single quote: '

If you want something easier to discern, you can use $() instead. It does the same thing, and also supports nesting, which the backtick does not.

So:

Code:
for temp in `ls -d */`
do
      cd $temp
done
or

Code:
for temp in $(ls -d */)
do
      cd $temp
done
or, without the command substitution, and without unnecessarily running ls:

Code:
for temp in */
do
      cd $temp
done

Last edited by zhangmaike; 10-30-2005 at 03:02 AM.
 
Old 10-30-2005, 04:32 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The latter:
Code:
for temp in */
doesn't works.
It should have been
Code:
for temp in */*
but that one is wrong too as it doesn't filter out non directories.
 
Old 10-30-2005, 12:07 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
just another idea, use this one a lot
Code:
prevdir="`pwd`"
find . -type d | while read dir; do
    cd "$dir"
    # do stuff
    echo `pwd`
    cd "$prevdir"
done
 
Old 10-30-2005, 01:54 PM   #5
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
As for my last suggestion not working... it seems to work fine (tested it on my computer). Replacing cd with echo, it lists all the directories in the current directory.

Last edited by zhangmaike; 10-30-2005 at 01:56 PM.
 
Old 10-30-2005, 02:31 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You are right, my mistake, the trailing slash is indeed filtering out non directories. Sorry for the confusion.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
$LINENO can't be modified in bash 3.0, while it can be in bash 2.05b Darwish Linux - Software 1 11-07-2005 02:57 PM
Bash Echo Kilo Linux - General 3 11-02-2005 11:11 PM
bash help brianbek Linux - General 2 04-03-2005 06:03 PM
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 07:53 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration