LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-23-2005, 09:52 PM   #1
tchoua
LQ Newbie
 
Registered: Feb 2005
Posts: 11

Rep: Reputation: 0
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
 
Old 02-23-2005, 10:06 PM   #2
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Assuming this is a shell script, you could just insert something like
Code:
cut --delimiter=/ --fields=2 $var1
Cheers,

mj
 
Old 02-24-2005, 03:05 PM   #3
tchoua
LQ Newbie
 
Registered: Feb 2005
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 02-24-2005, 03:09 PM   #4
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Perhaps you should post your script as it currently stands....

Cheers,

mj
 
Old 02-24-2005, 04:00 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
var3=`basename $var1`

Just need to get rid of the spaces ;)


Cheers,
Tink
 
Old 02-24-2005, 04:25 PM   #6
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Nice !

Cheers,
mj
 
Old 02-24-2005, 04:56 PM   #7
tchoua
LQ Newbie
 
Registered: Feb 2005
Posts: 11

Original Poster
Rep: Reputation: 0
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 ;-) )
 
Old 02-24-2005, 05:04 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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
 
Old 02-24-2005, 06:18 PM   #9
tchoua
LQ Newbie
 
Registered: Feb 2005
Posts: 11

Original Poster
Rep: Reputation: 0
thanks a lot.
it is clear in my head now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Writing a shell script mattz40 Debian 5 04-13-2005 06:56 PM
shell script writing ust Linux - General 6 01-24-2005 01:06 PM
writing my first shell script speedhead34 Linux - Newbie 4 12-20-2004 12:50 AM
help writing shell script np complete Linux - Newbie 6 08-30-2004 09:43 PM
Need Help in Writing a Shell Script. KptnKrill Linux - General 2 05-19-2003 05:44 AM


All times are GMT -5. The time now is 03:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration