Extract directory path from a parameter


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extract directory path from a parameter
# 1  
Old 01-08-2020
Extract directory path from a parameter

i was attempting to extract a directory path that was passed from a parameter with this code
Code:
vdir=`dirname $p1`
echo current directory $vdir

it does not work when the parameter passed has wild card on it.
for example
Code:
$ sh sample1.sh "/sbin/log/c*.log"
dirname: extra operand `/sbin/log/c1.log'
Try `dirname --help' for more information.
current directory
$

it works when not using a wild card (expanded file)
Code:
$ sh sample1.sh "/sbin/log/c1.log"
current directory /sbin/log
$

please help thank you.
# 2  
Old 01-08-2020
The wild card will be expanded in your script. It should work if exactly one single pattern matched; it fails if none or more than one matched.


Try a for loop in your script.
# 3  
Old 01-08-2020
that works thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What is the difference ../directory path and ./directory path in ksh?

What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 Replies

2. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

3. 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

4. Shell Programming and Scripting

How to extract strings from full path when full path is not fixed

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

5. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

6. Shell Programming and Scripting

How to extract and replace with parameter value?

Hi, I have a function getparam() which takes parameter name as input and parameter value as output. I have a strting like this, abcd#paramname1#efgh#paramname2#ijklmnopq Above one is my input, The strting enclosed in # are the parameter names. Here question is, I have to get the... (0 Replies)
Discussion started by: anandapani
0 Replies

7. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

8. Shell Programming and Scripting

Extract path within path

Hi, I have a environmental variables, ORACLE_HOME=/u01/oracle/ORCL/db/tech/10.2.0 ORACLE_SID=ORCL Now I need to create a variable and need to extract some part from ORACLE_HOME. I need to get the path from ORACLE_HOME till ORACLE_SID as /u01/oracle/ORCL. I may need to check also... (6 Replies)
Discussion started by: sreejitnair123
6 Replies

9. Shell Programming and Scripting

Extract directory from a file path

Im trying to extract a directory from a path entered by the user Lets say the path is path=/home/bliss/files/myfile.txt i wanna extract "/home/bliss/files" from $path ... how can i do this? (4 Replies)
Discussion started by: mrudula009
4 Replies

10. Shell Programming and Scripting

how to find the path of a file when it is passed as ....filename(parameter) to script

hi unix guru's..................:confused: question is posted in the #3 permalink shown below. (3 Replies)
Discussion started by: yahoo!
3 Replies
Login or Register to Ask a Question