getting the path part of an argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting the path part of an argument
# 1  
Old 03-22-2004
getting the path part of an argument

Seems I'm inundating this forum with questions, but anyway:

I am writing a script that should accept one and only one argument when called.

That argument should designate a file, either with path/filename or just filename.

Now to the difficult bit:

I want to figure out a way to store just the path in one variable and just the file name in another. I need this for subsequent manipulation.

I thought I should be able to do this with sed or awk, but I couldn't figure it out.
Also, I am fundamentally unsure whether it's a good idea to just look for the last "/" in the argument.

So I'm going to ask the one question I am starting to get famous for: is there a better way to achieve what I want?

The below code is what I currently have, giving you the context this is for, not tremendously important, but just so you can see where I'm going:
Code:
if [ $# -eq 1 ] && [ -r "$1" ] && [ -f "$1" ] && [ `wc -c "$1" | awk '{print $1}'` -gt 681574400 ]; then 
# I should add a check whether the current dir is writable 
	# there is only one parameter AND it\'s writable AND it\'s an ordinary file AND it\'s bigger than 650MB
	split -b650m "$1" "$1"'.carved.'
	#echo "last error:"
	#echo $?
	# check how split reacts when given a file smaller than 650MB
	
	slices=( $(ls . | grep "$1.carved." | grep -v "rejoin.$1") )
		
	# echo "Number of slices:"
	slice_count=${#slices[@]}
	# echo $slice_count
	
	echo "#!/bin/sh" > "rejoin.$1"
	index=1
	while [ "$index" -lt "$slice_count" ]; do
		echo "cat ${slices[$index]} >> ${slices[0]}" >> "rejoin.$1"
		let "index = $index + 1"
	done
	echo "mv ${slices[0]} \"$1\"" >> "rejoin.$1"
	echo "exit" >> "rejoin.$1"
	chmod 755 "rejoin.$1"
	echo "Generated rejoin script."

	exit 0;
else
	echo "carve - carves up a (large) file into 650MB pieces";
	echo
	echo "usage: carve <filename>";
	echo 
	echo "carve also generates a \"rejoin\" script for future reassembly"; 
	# That's because I want the user to have no excuse for not including a 
	# surefire rejoin script when putting these files somewhere.
	# We don't want some other user end up with a bunch of confusing files
	# s/he doesn't know how to deal with.
	# This "included" generation out of this script is also to minimize 
	# dependencies.
	exit 1
fi

# 2  
Old 03-22-2004
This works:

Code:
#!/bin/bash

INPUT="$1"

echo "Original : "${INPUT}
echo ""
echo "Without path : "${INPUT##/*/}
echo ""
echo "Path only : "${INPUT%/*}

$ cut-it /a/b/c/d/e/somefile
Original : /a/b/c/d/e/somefile

Without path : somefile

Path only : /a/b/c/d/e


Only works if a path is given with the filename....... But you might be able to work around that.

Should work for bash and ksh, check manpage (Parameter Expansion, near the bottom of the paragraph) for a more detailed explanation.

Hope it helps anyway Smilie
druuna
# 3  
Old 03-22-2004
Excellent! Good stuff!
Smilie

btw, I found it's actually better to use:
Code:
INPUT="$1"

echo "Original : "${INPUT}
echo ""
echo "Without path : "${INPUT##*/}
echo ""
echo "Path only : "${INPUT%/*}

as this will take into account the possibility of only having a relative path (and not an absolute one) -- as in cut-it somedirectory/filetobecut

Or is there a reason why you had the two forward slashes?

Also, I still need to account for the possibility of the path being just "/" -- and of course the possibility of there not being a path at all, but at least what you've showed me is a solid start. Many thanks for that! Smilie
# 4  
Old 03-22-2004
Try dirname
# 5  
Old 03-22-2004
Quote:
Or is there a reason why you had the two forward slashes?
If I'm honest: Didn't think of that.
I _always_ use absolute pathnames, that's why Smilie

Quote:
Also, I still need to account for the possibility of the path being just "/" -- and of course the possibility of there not being a path at all
You could do some basic checks on $1.

Something like this maybe (one line):
[[ ! -n $1 ]] || [[ $1 == "/" ]]&& echo "Supply (correct) filename please." && exit 1

If $1 is NULL OR $1 equals / then print the message and exit.
druuna
# 6  
Old 03-22-2004
Man, that felt a little bit like what must have felt to find out about wget Smilie

Seriously, now. Perderabo: you rock.
And that's not to forget or diminish your input, druuna -- thanks to both!

This is what I made out of this:

Code:
#!/bin/sh  
echo "original : "$1      
echo ""
filenameonly=`basename $1`    
echo "file name only : "$filenameonly
echo ""
pathonly=`dirname $1`"/"   
echo "path only : "$pathonly

and as you can see:
Code:
DigitalGoddess:~/bin ropers$ ./cut-it /mus/tard
original : /mus/tard

file name only : tard

path only : /mus/



it absolutely cuts the mustard. Smilie

PS:
That't probably the "worst" thing about UNIX: It's too powerful and too diverse. No matter whether you know little or lots -- you're bound to run across a situation where you write a fantastic piece of code to handle a certain task -- only to find out later that, well, someone else had that problem before and s/he coded a solution which may or may not even be part of your default distribution. Smilie But one can only learn along the way.
# 7  
Old 03-22-2004
A nice and 'clean' sollution, like it better then mine Smilie

That's one of the things I like about the whole *nix family: So many ways to do something and a few of those could be real beauties.
druuna
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

A way to print only part of directory path

Hi, So I struggled to find a solution to the following problem: I want to make sed print only part of multiple different paths. So lets say we have /path/path1/path2/logs/bla/blabla /path/path1/path2/path3/logs/yadda/yadda/yadda Can someone suggest a way to make sed or other... (5 Replies)
Discussion started by: dampio
5 Replies

2. Shell Programming and Scripting

Identify full path in argument

I have a small script to send copies of files to another computer used for tests but in the same location:pwd=`pwd` for i in "$@" do echo "rcp -p $i comp-2:$pwd/$i" rcp -p $i comp-2:$pwd/$i echo "Finished with $i" doneIs there a way I can check the parameter to see if it is a full... (5 Replies)
Discussion started by: wbport
5 Replies

3. Shell Programming and Scripting

Pass argument in script to run specific part in that

Hello Friends, I need you help ! I have a scripts names runsteps.sh which contains command to run bunch of commands for each application you want to install " Oracle " Jboss" etc echo " Which app you want to install Jboss" ? Yes or no? read ans depending on Yes or not it goes inside... (3 Replies)
Discussion started by: saurabh84g
3 Replies

4. Programming

Passing full path as argument when it contains variable strings

Hi, In directory "inoutfiles", I have folders fold0001, fold0002 and so on. Every folder has corresponding file file0001.txt, file0002.txt and so on. I want to perform a certain action on multiple files in one go. The cpp file is in the same directory as "inoutfiles". This is my code : ... (1 Reply)
Discussion started by: KidD312
1 Replies

5. UNIX for Advanced & Expert Users

Passing full path as argument when it contains variable strings

Hi, In directory "inoutfiles", I have folders fold0001, fold0002 and so on. Every folder has corresponding file file0001.txt, file0002.txt and so on. I want to perform a certain action on multiple files in one go. The cpp file is in the same directory as "inoutfiles". This is my code : ... (0 Replies)
Discussion started by: KidD312
0 Replies

6. UNIX for Advanced & Expert Users

How do i use vi to substitute an expression that is part of a path?

Let say I have /home/user1/bin/ and i want it to be /root/bin I tried : s/\/home\/user1/user2/ but it can't find the pattern user1. I tried: s/*\/home\/user1*/user2/ as well same result many thanks (4 Replies)
Discussion started by: c00lsnoopy
4 Replies

7. Shell Programming and Scripting

taking a part of string from a given path

Hi I have two path as follows system/console/bin/code/sample/testfile.txt system/console/bin/database/files/new/dbfile.txt I need the output as code/sample in first case database/files/new in second case That is I am omitting system/console/bin and the filename(s) in both... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

8. Shell Programming and Scripting

regex to select last part of a path

Hi all, I am learning the use of regular expression and I would like to know which regex can be used to select only the last part of a directory path name. Something like: /dir1/dir2/dir2 and I want to select the last /dir2 where dir2 can be any kind of string. Thanks a lot for your help.... (7 Replies)
Discussion started by: elric
7 Replies

9. Shell Programming and Scripting

retrieve part of file path

Hi I am trying to use sed to retrieve part of my html file's path. I am having a hard time getting what I want. Could someone give me some help? I want to retrieve the section after html and before the file name For example if I have the following, ... (3 Replies)
Discussion started by: tiger66
3 Replies

10. UNIX for Dummies Questions & Answers

Maximum length of a path given as an argument to a shell script

hi, I am using solaris10. I have to write a bourne shell script, which copies files for the said destination path which is passed as an argument to the script. it looks like this myscript.sh /var/test -->destination path now i would like to know what is the maximum length i can... (3 Replies)
Discussion started by: raghu.amilineni
3 Replies
Login or Register to Ask a Question