Command substitution inside of a variable expression (AIX, KORN) | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Command substitution inside of a variable expression (AIX, KORN)

Shell Programming and Scripting


Tags
aix, command substitution, korn shell, variable expansion

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-25-2013
sydox sydox is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 8 November 2013, 1:43 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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 -l

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:


Code:
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!

Thanks in advance for your help.

-SIdney
Sponsored Links
    #2  
Old 01-25-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 29 October 2014, 9:43 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
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  
Old 01-25-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 28 October 2014, 5:00 AM EDT
Posts: 330
Thanks: 36
Thanked 86 Times in 85 Posts
Your code works fine for me under Solaris.

Maybe try cd ${1:-$PWD} instead.
    #4  
Old 01-25-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 31 October 2014, 2:05 AM EDT
Location: Amsterdam
Posts: 9,561
Thanks: 286
Thanked 2,430 Times in 2,177 Posts
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  
Old 01-25-2013
sydox sydox is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 8 November 2013, 1:43 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 01-28-2013
sydox sydox is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 8 November 2013, 1:43 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 01-28-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 29 October 2014, 9:43 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 07:25 AM.