can you change direcotry using variables?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting can you change direcotry using variables?
# 1  
Old 07-02-2004
Question can you change direcotry using variables?

is this allowed?

cd ../$var


inside of a for loop?

I want to be able to go into a new folder in the same directory with each iteration of the loop. I want $var to change to the folder number I am trying to get into.
# 2  
Old 07-02-2004
Yes.
# 3  
Old 07-02-2004
Yes. It is allowed. You can change directories using variables. Below is sample code to do that in which directories has the list of directories that needs to be accessed to get information.

for var in `cat directories`
do
cd ../$var
pwd
done
# 4  
Old 07-04-2004
One point here - rather than using cat, I'd do

Code:
while read var 
do 
   cd ../$var
   #do something
done < directory_list

This saves us an unneeded "cat"

Just my two pence

Ta,
ZB
# 5  
Old 07-06-2004
Thanks! Getting closer...do any of you guys know a good reference website for syntax?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change the value of a variables?

Hi Room, i want to edit the value of a variable of one file using different file. for eg :- file1 name = "john" loc = "mmk" desig = "SSE" and i want to change the value of loc i.e mmk to bos. so after the change it should look like name = "john" loc = "bos" desig = "SSE"... (4 Replies)
Discussion started by: suraj1207
4 Replies

2. Shell Programming and Scripting

Unable to change environment variables in bash script

Hello! For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which... (4 Replies)
Discussion started by: markolopa
4 Replies

3. Shell Programming and Scripting

SED 4.1.4 - INI File Change Problem in Variables= in Specific [Sections] (Guru Help)

GNU sed version 4.1.4 on Windows XP SP3 from GnuWin32 I think that I've come across a seemingly simple text file change problem on a INI formatted file that I can't do with SED without side effects edge cases biting me. I've tried to think of various ways of doing this elegantly and quickly... (5 Replies)
Discussion started by: JakFrost
5 Replies

4. UNIX for Dummies Questions & Answers

change makefile environment variables

this is my first post so Hello, here is my question @top level Makefile should not set values for environment variables FC, CC, FFLAGS (etc) but use the ones that mpi_make sets. So as you can see i have to run an mpi program, in fortran and i am supposed to do the above.the program was... (3 Replies)
Discussion started by: Kwstas
3 Replies

5. UNIX for Dummies Questions & Answers

Why does set also change setenv variables?

I thought that set and setenv was easy enough to understand until I started experimenting. I noticed the same problem in a previous thread, so I will use it as an example. set command gave the following output: argv () cwd /homes/e/ee325328/assignment.2 home /homes/e/ee325328 path ( a... (2 Replies)
Discussion started by: benwj
2 Replies

6. Shell Programming and Scripting

'while' loop does not change local variables?!

(I think this question desearves separate thread..) I have a problem with 'while' I am trying to set variables by 'while' and it is fine inside, but after completting the loop all changes are lost: > bb="kkkk - 111\nlllll - 22222\nbbbb - 4444" > echo "$bb" kkkk - 111 lllll - 22222 bbbb -... (3 Replies)
Discussion started by: alex_5161
3 Replies

7. Shell Programming and Scripting

How to change parent shell's variables?

I have a question about how to change variables in parent shell. My script, test.sh, is below: #!/bin/sh # test.sh PATH=. The commands I ran for test: $ echo $PATH .:/usr/bin:/usr/sbin:/usr/local/sbin: $ sh test.sh $ echo $PATH .:/usr/bin:/usr/sbin:/usr/local/sbin: ... (1 Reply)
Discussion started by: pankai
1 Replies

8. Shell Programming and Scripting

Script look for file ,sort and copy to other direcotry

Hi I am in a situation to write a shell script that looks for the flat files continuosly and if files exists in that directory sort on the files and get the latest file(File comes with timestamp at the end) and copy the latest file to the other directory and again copy the next one to the same... (0 Replies)
Discussion started by: reddi22
0 Replies

9. UNIX for Dummies Questions & Answers

Return to the Home direcotry

Hi, New to the UNIX Environment and I think made some mistake, because my cd command is not returning me back to the home directory. It gives an error : does not exist Can anyone help me please. Please note that I have even tried cd $HOME but got the same error message. Thanks in... (4 Replies)
Discussion started by: shoeb_syed
4 Replies

10. UNIX for Advanced & Expert Users

Can't delete a direcotry

Hi, I don't see my new post, so I am post it again: I am having hard time to delete a directory: $ rmdir testdxdwdw $ rm -r testdoxdwdw rm: testoxdwdw non-existent $ rm vtestoxdwdw No such file or directory $ rm -rf vtestoxdwdw ### still don't work Thanks! (0 Replies)
Discussion started by: oradbus
0 Replies
Login or Register to Ask a Question