Change of directory thru script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change of directory thru script
# 1  
Old 03-26-2007
Change of directory thru script

Smilie
Hi All,
This script is not working. I want to change the directory as per users selection in current shell. Looks like it is spawning sub-shell internally.
I have used

. changedir.sh
source changedir.sh
./changedire.sh

But did not work. Currently shell directory remain the same.

Please correct my mistakes.
Thanks,
Ram

*************************
x=0
for tree in $( ls -l | grep "^d" | cut -c61- )
do
x=`expr $x + 1`
echo "$x $tree"
done
echo "Enter the view number to set or 0 to another view or q to quit"
read answer
y=0

for user in $( ls -l | grep "^d" | cut -c61- )
do
y=`expr $y + 1`
if [ $y == $answer ]; then
cd $user
echo "setting tree: $user"
fi
done
***************************
# 2  
Old 03-27-2007
Hi,
Sometimes getting the directory name by using cut -c61- may lead to error like it may not get the directory name properly.
Try the following code to get the directory name and use your code like this
ls -l | grep ^d | cut -d: -f2 | sed 's/[0-9][0-9] //g'

x=0
#for tree in $( ls -l | grep "^d" | cut -c61- ) Instead of this use
for tree in $( ls -l | grep ^d | cut -d: -f2 | sed 's/[0-9][0-9] //g' )
do
x=`expr $x + 1`
echo "$x $tree"
done
echo "Enter the view number to set or 0 to another view or q to quit"
read answer
y=0

If my assumption is right this will solve your issue...
Thanks
Raghuram
# 3  
Old 03-28-2007
you can use a function in your interactive shell...

here's a silly thing I use.
Put it in a file and source it and I have a CD command which
gives me a choice of my ENV directories.
(i am lazy)

Code:
function CD
{
unset list
    env | while read line;do
        var=${line%=*}
        val=${line#*=}
        [ -d $val ] && list="$list $var"
    done

    select dir in $list;do
        eval cd \$$dir
        break
    done
unset list
}

# 4  
Old 03-28-2007
Quote:
Originally Posted by bigearsbilly
you can use a function in your interactive shell...

here's a silly thing I use.
Put it in a file and source it and I have a CD command which
gives me a choice of my ENV directories.
(i am lazy)

Code:
function CD
{
unset list
    env | while read line;do
        var=${line%=*}
        val=${line#*=}
        [ -d $val ] && list="$list $var"
    done

    select dir in $list;do
        eval cd \$$dir
        break
    done
unset list
}

This is silly but really neat!
# 5  
Old 03-29-2007
I am a genius you know!
(so my mum says)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change directory within a bash shell script

Hi, I have been trying to execute the below command by changing directory and then copying contents of one directory to another by doing some file name manipulations in between. However this isnt working since as soon as the statement completes it goes back to the original folder. Can someone... (5 Replies)
Discussion started by: HikingLife
5 Replies

2. Shell Programming and Scripting

Script to Change Permission on a directory after every hour

I want to change the permission of a dir to 777 after every hour in a background process.I do not have the access to the crontab , is there another way of doing it a scrit of some thing like that . Any help will be great. (1 Reply)
Discussion started by: neeraj617
1 Replies

3. Shell Programming and Scripting

Script to change first line of files in directory

I need a script to take the filename of every file in a directory and substitute that file name for whatever is on the first line of the file. There may or may not be anything on the line, but I want the line to be the same as the file name. Most of the script tools I have used are non-destructuve,... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

4. Shell Programming and Scripting

How to change a directory in shell script?

HI, I need to change the working directory by using the shell script /Export/home/user_name I have to go one step back like /Export/home Please help on this.:confused: (3 Replies)
Discussion started by: thelakbe
3 Replies

5. Shell Programming and Scripting

directory change in shell script

Hi, I am trying to change the directory in my script, & want to check if the directory is present or not . Ex: cd /home/xyz/temp/logs if the logs directory is not present i want to show the error in script instead of shell script error. Can anybody please help me on the same Thx in... (2 Replies)
Discussion started by: vls1210
2 Replies

6. Shell Programming and Scripting

How to change a directory on windows via perl script

How to change a directory on windows via perl script. I wanna mount a share on windows via perl script. I have used:- system("dir"); gives the list of directories in the drive i am.This command works well. Now i want to move from c drive to z drive via perl script.I am using:- ... (2 Replies)
Discussion started by: TRUPTI
2 Replies

7. Linux

Change directory in a shell script

How do u change ur directory using a shell script?? (12 Replies)
Discussion started by: laxmi
12 Replies

8. Shell Programming and Scripting

change directory using shell script

How do u change ur directory using a shell script?? (0 Replies)
Discussion started by: laxmi
0 Replies

9. Shell Programming and Scripting

Change Directory via a script?

I would like to have a script that would change my current working directory. However, any time I execute a 'cd' command in a script, it holds only for the life of that script -- the working directory on exit is the same as when the script was initiated. Is it possible to have the script return... (3 Replies)
Discussion started by: George Borrmann
3 Replies
Login or Register to Ask a Question