|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Command substitution inside of a variable expression (AIX, KORN)
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: Code:
#!/usr/bin/ksh Code:
cd ${1:-`pwd`}; pwd; ls -lI 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: Code:
cd ${1:-.}; pwd; ls -lThe 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! Thanks in advance for your help. -SIdney |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
I don't have access to an AIX machine right now, but I couldn't reproduce the behavior you describe with ksh M-11/16/88i and M-12/28/93d on Solaris. Could you execute the script like this and post the output? Code:
/usr/bin/ksh -xv <script_name> |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Your code works fine for me under Solaris.
Maybe try cd ${1:-$PWD} instead. |
|
#4
|
||||
|
||||
|
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: Code:
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. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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. Once again thanks and enjoy your weekends! -Sidney |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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: Code:
/usr/bin/ksh -xv scd / I get: Code:
+ cd / pwd / + ls -l Then my files..... Then I do pwd and get: Code:
/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: Code:
scd I get: Code:
/ files in root under ls -l.... Could this just be an anomoly? |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
This is an expected behavior. If you want to modify your current path, you should use a function or source the script: Code:
. ./<script_name> <args> |
| Sponsored Links | ||
|
![]() |
| Tags |
| aix, command substitution, korn shell, variable expansion |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Variable inside command substitution | drbiloukos | UNIX for Dummies Questions & Answers | 3 | 10-04-2011 08:24 AM |
| [Solved] Command Substitution and Variable Expansion within a Case | jaimielives | Shell Programming and Scripting | 1 | 01-12-2011 04:06 AM |
| Not able to store command inside a shell variable, and run the variable | gvinayagam | Shell Programming and Scripting | 3 | 11-19-2010 05:59 AM |
| How to use variable with command substitution in variable | rajukv | Shell Programming and Scripting | 1 | 08-16-2010 08:50 AM |
| sed insert command and variable expansion/command substitution | glev2005 | UNIX for Dummies Questions & Answers | 8 | 03-09-2010 02:18 PM |
|
|