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 06-06-2009, 01:19 PM   #1
pkacker
LQ Newbie
 
Registered: Jun 2009
Location: Genova, Italy
Posts: 13

Rep: Reputation: Disabled
cd command with variable


I want to automate a process, done by a specific software. I want software do this job in the respective directories. I have some 94 directories and each directory have the respective files on which calculation needs to be done. The script that I wrote, call another file(autodock) containing instructions for specific action made on the files. But the my script not working properly.It is not changing the directory (cd command with the variable)
Here is my script:

#!/bin/bash
#this scrip automate the docking process
for DIR in `ls -ltr | awk '/^d/' | awk '{print $8}'`
do
cd ${DIR}
./autodock
done


the error is:
mazingaz:/home/puneet/Desktop/test_dock> ./ready
./ready: line 6: ./autodock: No such file or directory
./ready: line 5: cd: 2QZK: No such file or directory
./ready: line 6: ./autodock: No such file or directory

The variable seems to be working properly but why the cd is not working..far from my understanding

Please suggest


Thanks
 
Old 06-06-2009, 01:31 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have to use absolute path, either for the script ./autodock and for the directories. If you cd into one directory, you cannot change to another directory in the list if you don't go back into the previous one. And if you launch autodock from each directory a copy of it should be inside all of them. Here is a solution:
Code:
for DIR in $(find . -type d -exec readlink -f {} \;)
do
  cd ${DIR}
  /full/path/to/autodock
done
 
Old 06-06-2009, 01:31 PM   #3
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
I would put something like
cd ..
before "done".

I mean, you got into a directory, you should get out before entering another one.

Last edited by Uncle_Theodore; 06-06-2009 at 01:38 PM.
 
Old 06-06-2009, 01:38 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Uncle_Theodore View Post
I would put something like
cd ..
before "done".
It should not be enough if the directory listing includes multiple levels of the subdirectories tree. Eventually
Code:
cd -
could do the trick.
 
Old 06-06-2009, 01:40 PM   #5
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Quote:
Originally Posted by colucix View Post
It should not be enough if the directory listing includes multiple levels of the subdirectories tree. Eventually
Code:
cd -
could do the trick.
If the OP wants multiple levels, his simple loop will not work anyway. He'll need a recursion. With a single list of dirs returned by his command, there's just one level.
 
Old 06-06-2009, 01:42 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Uncle_Theodore View Post
If the OP wants multiple levels, his simple loop will not work anyway. He'll need a recursion. With a single list of dirs returned by his command, there's just one level.
Correct. I confused -r with -R. Sorry.
 
Old 06-06-2009, 01:47 PM   #7
pkacker
LQ Newbie
 
Registered: Jun 2009
Location: Genova, Italy
Posts: 13

Original Poster
Rep: Reputation: Disabled
cd command with variable

yeah..

with cd .. it is working perfectly..

thanks a lot
 
Old 06-06-2009, 01:51 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pkacker View Post
yeah..

with cd .. it is working perfectly..

thanks a lot
And do you have the script inside every directory???
 
Old 06-06-2009, 02:40 PM   #9
pkacker
LQ Newbie
 
Registered: Jun 2009
Location: Genova, Italy
Posts: 13

Original Poster
Rep: Reputation: Disabled
yes a copy of autodock script i have in each directory..
 
  


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
command out to a variable zebrafx Programming 4 02-20-2009 09:12 AM
~/.bashrc variable for a command? Maybe? computer_freak_8 Linux - Newbie 17 02-02-2009 07:46 PM
variable in a tail command jadeddog Programming 4 06-29-2008 09:35 PM
parsing a variable from here command tostay2003 Programming 5 12-26-2007 08:17 PM
How do I turn my variable into a command? spiffytech Linux - Software 3 12-29-2005 12:30 PM

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

All times are GMT -5. The time now is 04:11 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