I have to write a script called trying. The script takes a full path (like /users4/st/jdoe/prog.c) as an argument and displays the path and the file name. For /users4/st/jdoe/prog.c, the path is /users4/st/jdoe and the file name is prog.c.

I know that I can use grep for it, but it's better to use string operators. Anyone has any ideas?

Hi!

I guess what you are looking for is basename and dirname.

$ dirname /users4/st/jdoe/prog.c
/users4/st/jdoe
$ basename /users4/st/jdoe/prog.c
prog.c

Regards, mawe

Hey There

It depends where you put it. If it's in a script and isn't on the #!/bin/whatever shebang line, then it denotes a comment (perl, all shells).

If you mean $# - it resolves to the number of arguments you've passed to your script.

Hope that helps :)

Mike

I have to write a script called trying. The script takes a full path (like /users4/st/jdoe/prog.c) as an argument and displays the path and the file name. For /users4/st/jdoe/prog.c, the path is /users4/st/jdoe and the file name is prog.c.

I know that I can use grep for it, but it's better to use string operators. Anyone has any ideas?

for large number of files, basename/dirname may not be a good choice ( but its up to you). Using bash string operation may be more efficient as you don't need to call "external commands"
eg

# s="/users4/st/jdoe/prog.c"
# echo ${s%/*}
/users4/st/jdoe
# echo ${s##*/}
prog.c
This article has been dead for over six months. Start a new discussion instead.