LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   writing a shell script (http://www.linuxquestions.org/questions/linux-general-1/writing-a-shell-script-294128/)

tchoua 02-23-2005 09:52 PM

writing a shell script
 
hi,
I am writing a script and I need to do something that I don't know how to do it.
here it is:
I have 2 variable $var1 and $var2
var1=directory/file_name
var2=directory/

I need to set a third viarible $var3 to file_name so if I do :
echo "$var3"
I get on the screen :
file_name

thanks for your help

mjrich 02-23-2005 10:06 PM

Assuming this is a shell script, you could just insert something like
Code:

cut --delimiter=/ --fields=2 $var1
Cheers,

mj

tchoua 02-24-2005 03:05 PM

sory but I can't get it to work succesfuly :
cut --delimiter=/ --fields=2 echo $var1
echo "$var1"

I get this error message :
cut: echo: No such file or directory
cut: directory/file 1.txt
directory/file 2.txt: No such file or directory

mjrich 02-24-2005 03:09 PM

Perhaps you should post your script as it currently stands....

Cheers,

mj

Tinkster 02-24-2005 04:00 PM

var3=`basename $var1`

Just need to get rid of the spaces ;)


Cheers,
Tink

mjrich 02-24-2005 04:25 PM

Nice !

Cheers,
mj

tchoua 02-24-2005 04:56 PM

var3=`basename $var1`
it works thanks.
what is basename for ? I look into the man but didn't understand it (I'm french sory about it ;-) )

Tinkster 02-24-2005 05:04 PM

Quote:

Originally posted by tchoua
var3=`basename $var1`
it works thanks.
what is basename for ?
It strips all leading directories (and optionally the extension)
of a fully qualified pathname ... e.g.
var1="/usr/X11/lib/fonts/font.dir"
basename will return font.dir by default ...

Quote:

I look into the man but didn't understand it
(I'm french sory about it ;-) )
C'mon - being french is not THAT bad ;)

Cheers,
Tink

tchoua 02-24-2005 06:18 PM

thanks a lot.
it is clear in my head now.


All times are GMT -5. The time now is 06:07 AM.