Korn Shell Help Needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell Help Needed
# 1  
Old 12-05-2005
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  
Old 12-05-2005
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  
Old 12-05-2005
I did reply, but Vinos is much more informative so I'll leave it to the pros.
# 4  
Old 12-05-2005
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  
Old 12-05-2005
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  
Old 12-05-2005
Thanks alot vino!

Regarding
Code:
rm current_path

I was trying to unset the variable.

cheers
Steve
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Needed with File Checker Korn Shell Script

I am attempting to write a korn shell script that does the following, but I am getting errors. I'm new to korn shell scripting and am not sure what I am doing wrong. Please help with example scripts. Thanks. 1) check for day of the week 2) if day of the week is Monday then check for the... (4 Replies)
Discussion started by: ijmoore
4 Replies

2. Shell Programming and Scripting

Help needed in Korn Shell scripting

#! /bin/ksh while read line do if ] ; then echo "no data" continue; fi echo "performing operation on $line" done < prg.txt (3 Replies)
Discussion started by: Juhi Kashyap
3 Replies

3. Shell Programming and Scripting

Bourne shell & Korn shell

Could some one tell me the difference btw Bourne shell and the Kshell? Which is more flexible and reliable in terms of portability and efficiency. When i type the following command .. $ echo $SHELL yields me /bin/sh Does this tells me that I am in Bourne shell. If yes, how can i get... (6 Replies)
Discussion started by: bobby1015
6 Replies

4. Shell Programming and Scripting

How to activate Korn Shell functionnalities in Bourne Shell

Hi All I have writing a Korn Shell script to execute it on many of our servers. But some servers don't have Korn Shell installed, they use Borne Shell. Some operations like calculation don't work : cat ${file1} | tail -$((${num1}-${num2})) > ${file2} Is it possible to activate Korn Shell... (3 Replies)
Discussion started by: madmat
3 Replies

5. Shell Programming and Scripting

korn shell

I am using korn shell but I want to have my prompt to represnent that of my C shell because I like it better. Is there anyway to do this? (1 Reply)
Discussion started by: vthokiefan
1 Replies

6. Shell Programming and Scripting

Korn Shell script needed

Hi all, I need a Korn Shell script that will go out to all Unix Servers and pull all non humans IDs from them while at the same time produce a file to show which servers we have access to and which ones we don't. Thanks in advance. (5 Replies)
Discussion started by: vinsara
5 Replies

7. Shell Programming and Scripting

Help needed in character replacement in Korn Shell

How do I replace a space " " character at a particular position in a line? e.g. I have a file below $ cat i2 111 002 A a 33 0011 B c 2222 003 C a I want all the 1st spaces to be replaced with forward slash "/" and the 3rd spaces to have 5 spaces to get the output below: 111/002... (8 Replies)
Discussion started by: stevefox
8 Replies

8. Shell Programming and Scripting

how to convert from korn shell to normal shell with this code?

well i have this code here..and it works fine in kornshell.. #!/bin/ksh home=c:/..../ input=$1 sed '1,3d' $input > $1.out line="" cat $1.out | while read a do line="$line $a" done echo $line > $1 rm $1.out however...now i want it just in normal sh mode..how to convert this?... (21 Replies)
Discussion started by: forevercalz
21 Replies

9. Shell Programming and Scripting

KORN Shell - Spawn new shell with commands

I want to be able to run a script on one server, that will spawn another shell which runs some commands on another server.. I have seen some code that may help - but I cant get it working as below: spawn /usr/bin/ksh send "telnet x <port_no>\r" expect "Enter command: " send "LOGIN:x:x;... (2 Replies)
Discussion started by: frustrated1
2 Replies

10. Shell Programming and Scripting

Korn Shell

Hi I am new to shell programming. I need help to write a script to monitor a process on Sun OS. If the process fails then call a oracle procedure. i check the process if running by typing ps -ef | grep ESP | grep -v grep root 29002 1 0 Mar 18 ? 7:20... (4 Replies)
Discussion started by: gpanesar
4 Replies
Login or Register to Ask a Question