Hello all. This is my first post/question on this site.
I�m a new Systems Analyst with previous experience with BASH. Although now I'm using AIX, and I�m trying to get a feel for the Korn shell (for those of you that don�t know AIX only uses the KORN shell).
I hope I put this into the correct forum topic; if not I apologize!
I am trying to use a command substitution inside of a variable expression:
#!/usr/bin/ksh
cd ${1:-`pwd`}; pwd; ls -l
[COLOR=black][FONT=Verdana]I want it to cd to a directory that is provided as an argument, if no directory is provided then i want cd to default to the current directory. Then: pwd; ls -l
It�s pretty straight forward. This works if a directory is provided; where I'm getting caught is that if no argument is provided cd goes to the home directory, as if :- didn't default to `pwd`. I have also tried:
cd ${1:-.}; pwd; ls -l
The pwd and ls �l part of the code works with no problem. Also to note: I was talking to a co-worker and they commented that due to working under AIX that perhaps it doesn�t support variable expressions. If that is the case then fine, now I know!
This probably has to do with CDPATH . in ksh it either needs to be empty or contain one empty path in order to be able to cd to a relative directory.
man ksh:
The shell variable CDPATH defines the search path for the direc-
tory containing arg. Alternative directory names are separated by a colon (:). The default path is <null>
(specifying the current directory). Note that the current directory is specified by a null path name, which
can appear immediately after the equal sign or between the colon delimiters anywhere else in the path list. If
arg begins with a / then the search path is not used. Otherwise, each directory in the path is searched for
arg.
Thanks for your SUPER quick response! Unfortunately I'll have to wait till Monday to try your suggestions.
radoulov: Thats a good idea!
Subbeh: I don't believe that PWD is considered an environment variable, but I'll have to double check.
Scrutinzer: Wasn't aware of CDPATH of being a variable either although I have yet to come across a reason to need it either.
I figured out where the problem lies. "scd" is the script that I'm tring to run, and I'm in my home directory and enter:
/usr/bin/ksh -xv scd /
I get:
+ cd /
pwd
/
+ ls -l
Then my files.....
Then I do pwd and get:
/usr/home/sydox
When the program encounters "cd ${1:-`pwd}" it changes the directory for the script but doesn't stay there. So when i type my cmmand with no argument it does what I expect it to and defaults to the current directory.
If I in root ( / ) and type:
WOW that worked!!
Please forgive my ignorance, but what does that extra "." do? I'm aware that "." signifies your current directory.
If you don't want to explain fully and have a good reference for me to read I'm VERY open to that.
Thanks for all of your help again!!!
With . filename [arguments] (or source filename [arguments] ) you read and execute commands from filename in the current shell environment
so that cd affects your current shell, otherwise the script is executed in a subshell and the parent shell environment remains unaffected.