CD (change directory) - Not advisable for executing a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CD (change directory) - Not advisable for executing a command
# 1  
Old 05-27-2009
Lightbulb CD (change directory) - Not advisable for executing a command

Hi,

This is a quick one.
I have got a review comment in one of the scripts that i wrote:

"In UNIX script it not advisable to use cd to a directory and then run command."

Is this true?
I was trying to cd to log directory and use some cat or head or ls command.

Many Thanks,
Sam
# 2  
Old 05-27-2009
Well, this is partially true. You may get confused when executing the script from another location or you may need to navigate back to the original folder.
You should rather use the full path to the file, i.e. 'cat /home/bob/test.txt'
Example :
Code:
cd /folder/test/
cat file1.txt
cd - 
# return to the last folder, variable kept in ENV as OLDPWD.
# or 
cd /home/bob/test/firstfolder/secondfolder/
cat somefile.txt

You can simply avoid all those 'cd' commands by just using the full path to the file.
# 3  
Old 05-27-2009
another reason is you may be victim of spoofing (if . is in the PATH...) and other side effects (often with JAVA stuff...) like a program runs when you cd to the directory but fails when called from elsewhere (environment issue)...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing a command line in a directory and its subdirectories

I'm finally about to make the jump from PHP's mysql driver to the newer mysqli one using the mysqli converter tool. The tool is called by this command line: php /usr/share/nginx/html/rather/converter/cli.php -d -p ".php,.shtml,.inc" -u -b -v -w >> /tmp/convert.log e.g. at the web root:... (2 Replies)
Discussion started by: dheian
2 Replies

2. UNIX for Dummies Questions & Answers

Executing profile from any directory other than $HOME

Is there a way to run the profile which is not in $HOME directory? then whats the command for that? (1 Reply)
Discussion started by: SKhan
1 Replies

3. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

4. Shell Programming and Scripting

Change color or font when executing a script

I'm writing a simple menu script and I would like to output the whole script using background color: black AND foreground color:white. Meaning if the ssh terminal of a user is set to green for example, it will change to my desired color when menu script is executed. Once the script is exited, it... (1 Reply)
Discussion started by: lhareigh890
1 Replies

5. Shell Programming and Scripting

How can I get the directory of my executing script?

hi, I tried whence $0, but that just gives me the filename relative to where I am executing the script from. I need to get the full pathname. thanks (19 Replies)
Discussion started by: JamesByars
19 Replies

6. Shell Programming and Scripting

getting : No such file or directory while executing a shell script

Hi all, I am getting : No such file or directory while executing a shell script. But i have that corresponding file in the corresponding path. It also have executable rights. Please help me out in this Thanks in advance. Ananthi.U (7 Replies)
Discussion started by: ananthi_ku
7 Replies

7. Shell Programming and Scripting

Change directory command (cd)

What's wrong with this script????? Please help!!! !#/bin/sh echo "please enter dir" read input cd $input I'm trying to make a simple script to change dir. But does not work. it stays at the same dir where i run the script. What did i do wrong?? (21 Replies)
Discussion started by: c00kie88
21 Replies

8. Shell Programming and Scripting

Executing script's directory

I have a shell script /home/user1/bin/sh1, how can I print out the directory of this file inside the script. Say I am in /home/user1/, then I run /home/user1/bin/sh1 I need it to print out /home/user1/bin. Thanks. (2 Replies)
Discussion started by: jasony001
2 Replies

9. Shell Programming and Scripting

Monitoring a directory for new files with .xx and executing command if found

Hi Guys. I am a complete shell scripting newbie with some syntax and commands understanding. I'm more of a win admin. With that said: I need to write a shell script to monitor a directory '/Mon_Dir' for new occurrences of files with .xx extension. Once a new file is detected in the directory, a... (4 Replies)
Discussion started by: krkan
4 Replies

10. Shell Programming and Scripting

Change directory command (cd)

I want to limit the directories that a user can access. When this users logs in, I do not want them to cd to any directory but those in their $HOME. In other words: login: pwd: /home/user cd / -- user tries to cd to root directory. pwd /home/user -- user is still in home... (4 Replies)
Discussion started by: oscarr
4 Replies
Login or Register to Ask a Question