Need Help in going to ~ directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help in going to ~ directory
# 1  
Old 10-06-2008
Need Help in going to ~ directory

Hi,
I have a variable "COMPBD=~zzzdba/comps" which i want to read and substitute in my script like cd $COMPBD. But it didnt work like that....Please can somebody throw some light on this....

thanks...
-Prasanna.K
# 2  
Old 10-06-2008

What does "didnt work like that" mean?

What did happen?

What, exactly did you do?

Is there an account named zzzdba?

Did you quote the variable when you used it? If not, it would have failed if there were spaces in the name of the directory.
# 3  
Old 10-06-2008
Which shell are you using?
Assuming there is an account in passwd file called zzzdba, and a directory under the user's home directory called comps, and you have permissions to "cd" to that directory.

This behaviour is a feature of ksh (see man pages). Tilde substitution only works on unquoted strings.
This works:
cd ~myname
This doesn't work:
cd "~myname"

In your case this should work.
COMPBD=~zzzdba/comps
cd ${COMPBD}
You can check the substitution.
echo "${COMPDB}"

If it still doesn't work maybe you need to force ksh (or a shell which supports tilde substitution) on the very first line of your script.
#!/bin/ksh
# 4  
Old 10-06-2008
Quote:
Originally Posted by methyl
Which shell are you using?
Assuming there is an account in passwd file called zzzdba, and a directory under the user's home directory called comps, and you have permissions to "cd" to that directory.

This behaviour is a feature of ksh (see man pages). Tilde substitution only works on unquoted strings.

It is a feature of all standard Unix shells, as well as tcsh (and probably csh, but I don't have a copy to test with).
Quote:

This works:
cd ~myname
This doesn't work:
cd "~myname"

But if it's a variable, the tilde expansion is done during the assignment, and the variable can, and should be, quoted.
Quote:
In your case this should work.
COMPBD=~zzzdba/comps
cd ${COMPBD}

That will fail if there's a space in the directory name. Use:

Code:
cd "$COMPBD"

Quote:
You can check the substitution.
echo "${COMPDB}"

If it still doesn't work maybe you need to force ksh (or a shell which supports tilde substitution) on the very first line of your script.
#!/bin/ksh
# 5  
Old 10-06-2008
i tried the below..
path=`cat /bridge/profiles/Z1000 | grep 'COMPBD=' | cut -d"=" -f2`
echo $path
~zzzdba/comps

cd $path
ksh: ~zzzdba/comps: not found

cd ${path}
ksh: ~zzzdba/comps: not found

cd "${path}"
ksh: ~zzzdba/comps: not found


Please suggest any idea.....
# 6  
Old 10-06-2008
Quote:
Originally Posted by cfajohnson

What does "didnt work like that" mean?

What did happen?

What, exactly did you do?

Is there an account named zzzdba?

Did you quote the variable when you used it? If not, it would have failed if there were spaces in the name of the directory.
i tried the below..
path=`cat /bridge/profiles/Z1000 | grep 'COMPBD=' | cut -d"=" -f2`
echo $path
~zzzdba/comps

cd $path
ksh: ~zzzdba/comps: not found

cd ${path}
ksh: ~zzzdba/comps: not found

cd "${path}"
ksh: ~zzzdba/comps: not found


Please suggest any idea.....
# 7  
Old 10-06-2008
Quote:
Originally Posted by kprasanna_79
i tried the below..
path=`cat /bridge/profiles/Z1000 | grep 'COMPBD=' | cut -d"=" -f2`

UUOC; use the filename as an argument to grep.
Quote:
echo $path
~zzzdba/comps

Where have you allowed the shell to expand it? Nowhere!

Tilde expansion is not performed on variables.

Use eval:

Code:
eval "path=$path"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

2. AIX

How to set owner and permission for files/directory in directory in this case?

Hi. My example: I have a filesystem /log. Everyday, log files are copied to /log. I'd like to set owner and permission for files and directories in /log like that chown -R log_adm /log/* chmod -R 544 /log/*It's OK, but just at that time. When a new log file or new directory is created in /log,... (8 Replies)
Discussion started by: bobochacha29
8 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

5. Web Development

Directory index forbidden by Options directive error on specific directory with indexing disabled

I am seeing the following error appear numerous times in my Apache error log: I have my Apache config configured as below, so I would expect indexing not to occur on this directory as it falls under the parent /web directory. Strangely all the IP address, including this example, all... (5 Replies)
Discussion started by: crmpicco
5 Replies

6. Shell Programming and Scripting

Catching the xml tag when only parent directory is known ..not the actual directory

Hi folks, I have an query that is let say i have to search in an xml file an tag that is <abcdef> now this xml file is at /opt/usr/local so one fastest way to achieve this is go to this location by cd /opt/usr/local and then do grep like this... grep -i abcdef but for this I must know the... (4 Replies)
Discussion started by: punpun66
4 Replies

7. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

8. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

9. Solaris

Restricting SFTP user to a defined directory and home directory

Hi, I've created solaris user which has both FTP and SFTP Access. Using the "ftpaccess" configuration file options "guest-root" and "restricted-uid", i can restrict the user to a specific directory. But I'm unable to restrict the user when the user is logged in using SFTP. The aim is to... (1 Reply)
Discussion started by: sftpuser
1 Replies
Login or Register to Ask a Question