The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help needed in character replacement in Korn Shell stevefox Shell Programming and Scripting 8 03-30-2007 12:59 AM
bourne shell or korn shell? XZOR UNIX for Dummies Questions & Answers 2 10-06-2006 03:34 AM
how to convert from korn shell to normal shell with this code? forevercalz Shell Programming and Scripting 21 11-23-2005 02:18 AM
KORN Shell - Spawn new shell with commands frustrated1 Shell Programming and Scripting 2 04-20-2005 03:23 PM
Korn Shell gpanesar Shell Programming and Scripting 4 03-25-2005 05:46 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-05-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Question Korn Shell Help Needed

How do I change directories to a path given by input variable in Korn Shell?

e.g. I tired with the Korn Shell below but it doesn't work.
----------------------------------
#!/bin/ksh
echo "Enter folder name: \c"
read folder
cd $folder
----------------------------------

Any help will be appreciated.
  #2 (permalink)  
Old 12-05-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
It depends on the way you invoke the script. I think you are invoking the script as ./steve.ksh

Look at this demo.


Code:
sh-2.05b$ cat steve.ksh 
#!/bin/ksh
echo -n "Enter folder name:"
read folder
cd $folder 
sh-2.05b$ pwd
/tmp
sh-2.05b$ ./steve.ksh 
Enter folder name:/tmp/steve
sh-2.05b$ pwd
/tmp
sh-2.05b$ . ./steve.ksh 
Enter folder name:/tmp/steve
sh-2.05b$ pwd
/tmp/steve
sh-2.05b$

One way is to invoke it as
Code:
. ./steve.ksh

  #3 (permalink)  
Old 12-05-2005
casphar casphar is offline
Registered User
  
 

Join Date: Dec 2005
Location: Vancouver, Canada
Posts: 13
I did reply, but Vinos is much more informative so I'll leave it to the pros.
  #4 (permalink)  
Old 12-05-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Question

Thank you vino and casphar!
I was invoking the script just by entering the name of the script (i.e. without "./" or ". ./").

I was trying to use this inside the code below (invoking it by . ./script.ksh)



Code:
#!/bin/ksh

echo "Enter folder name: \c"
read folder

#Remember current path to go back when loop finishes

pwd > current_path

#Start Loop

for file in `ls $folder`
do
      cd $folder
      mv $file new_$file
      cd | echo $current_path
done

rm current_path

... and it does modify the files in the given directory (folder_name) correctly however it gives the error below:

ksh: current_path: parameter not set
ksh: folder_name: not found
ksh: current_path: parameter not set
ksh: folder_name: not found
ksh: current_path: parameter not set
ksh: current_path: parameter not set
rm: current_path non-existent

Could someone tell me why I'm getting this error and how I can stop this error to appear?
  #5 (permalink)  
Old 12-05-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Modifying your script...


Code:
#!/bin/ksh

echo "Enter folder name: \c"
read folder

#Remember current path to go back when loop finishes
current_path=$(pwd)

#Start Loop
cd $folder

for file in *
do
      mv $file new_$file
done

cd $current_path

That should work.

I dont understand what you are trying to do with the following:

Code:
rm current_path

Remove the directory or unset the variable ?
  #6 (permalink)  
Old 12-05-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Thanks alot vino!

Regarding
Code:
rm current_path

I was trying to unset the variable.

cheers
Steve
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0