Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confused ... cd to dir in a ksh script 1

Status
Not open for further replies.

podsds

MIS
Nov 8, 2002
20
US
So I have a ksh script that needs to be in a certain dir before executing another script. I tried to code the cd command using a variable and it does not work ... what am I doing wrong? Below is a copy of a simple script put together. Notice that line 9 does not work while and line 14 does work. Also notice that line 15 and 20 do not work, but they previously wored in lines 7 and 11. So I am confused.

Any idesa?

1#!/bin/ksh
2#set -x
3CD=/usr/bin/cd
4ECHO=/usr/bin/echo
5PWD=/usr/bin/pwd
6${ECHO} >> ${log_name}
7${ECHO} "Test 1 Current Directory is `${PWD}`" >>
${log_name}
8${ECHO} >> ${log_name}
9${CD} /export/home
10${ECHO} >> ${log_name}
11${ECHO} "Twst 2 Current Directory is `${PWD}`" >>
{log_name}
12${ECHO} >> ${log_name}
13cd /export/home
14${ECHO} >> ${log_name}
15${ECHO} "Test 3 Current Directory is `${PWD}`" >>
${log_name}
16${ECHO} "Test 3 Part 2 Current Directory is `/usr/bin/pwd`"
>> ${log_name}
17${ECHO} >> ${log_name}
18/usr/bin/cd /export
19${ECHO} >> ${log_name}
20${ECHO} "Test 4 Current Directory is `${PWD}`" >>
${log_name}
21${ECHO} "Test 4 Part 2 Current Directory is `/usr/bin/pwd`"
>> ${log_name}
22${ECHO} >> ${log_name}

Output from script execution, itheratice at terminal

Test 1 Current Directory is /export/home/sysadmin/script/common


Twst 2 Current Directory is /export/home/sysadmin/script/common


./sds[15]: /export/home: cannot execute
Test 3 Current Directory is
Test 3 Part 2 Current Directory is /export/home


./sds[20]: /export/home: cannot execute
Test 4 Current Directory is
Test 4 Part 2 Current Directory is /export/home

 
ksh's 'cd' is a builtin command.
using /usr/bin/cd executes the command (/usr/bin/cd) in the subshell NOT changing the effective directory of you current shell.


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I can accept this. So just use cd as a builtin no problem.

What do you mape of the PWD in line 15 and 20 ... is it the built in too?
 
PWD is an environment variable which changes depending upon your current location on the box. HTH.
 
all your [{}] are superfluos and confusing,
learn please about the use of [{}] in sh-script.
aaa=aaa
echo ${aaa} # is not wrong NOR necessary
echo ${aaa}.bbb # is not wrong NOR necessary
echo ${aaa}_bbb # is necessary !!!
echo ${aaa}bbb # is necessary !!!
 
Thanks for the lession, but the superflus [{}] don't really bother me, in fact they help me spot variable with in the code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top