Sponsored Content
Top Forums Shell Programming and Scripting getting the path part of an argument Post 48946 by ropers on Monday 22nd of March 2004 11:38:17 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 06:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy