LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-21-2012, 04:01 PM   #1
brian00
Member
 
Registered: Aug 2011
Posts: 33

Rep: Reputation: Disabled
basename command in linux


when I tried to identified a variable I got the following error:


MYSP=`basename $0`
basename: invalid option -- k
Try `basename --help' for more information.


what is the right syntax?

Thanks,
 
Old 02-21-2012, 04:04 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
First let's see output from:
Code:
echo $0
(within the script you're running the basename(1) command in!)

Or at least run your script with set -x.
 
Old 02-21-2012, 04:14 PM   #3
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
thanks, all I want to do is to define a variable within my korn shell script. so obviously (echo $MYSTUFF) won't work b/c I can't even define the variable.

Thoughts?

Thanks again
 
Old 02-21-2012, 04:16 PM   #4
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Brian, you are suffering from a confusion of ideas. $0 is a positional parameter. If you are setting 0=something, you have a problem. Please do as anomie says. He's not asking you to echo $MYSP in which case you would be right, there'd be no point.

$0 must have something like -k ?
Code:
basename -k
basename: invalid option -- 'k'
Try `basename --help' for more information.
 
Old 02-21-2012, 04:16 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
You're passing an argument to basename(1), and we need to be able to see what that argument is in order to be able to help. So, try this:

Code:
#!/bin/ksh

set -x

foo=$(basename $0)

exit 0
What output does it produce when you run it? That should show us what $0 is being populated with. My guess is your script file is strangely named.
 
Old 02-21-2012, 04:18 PM   #6
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Quote:
Originally Posted by anomie View Post
What output does it produce when you run it? That should show us what $0 is being populated with. My guess is your script file is strangely named.
I had thought so too. But naming that script -k, at least in bash, doesn't produce that behavior.
 
Old 02-21-2012, 04:28 PM   #7
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Code:
#!/bin/ksh

typeset var="$0"
typeset var2=`basename $0`
echo "$var"
echo "$var2"

Last edited by uhelp; 02-21-2012 at 04:31 PM.
 
Old 02-21-2012, 04:31 PM   #8
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by jhwilliams
I had thought so too. But naming that script -k, at least in bash, doesn't produce that behavior.
I've tried a variety of fun names and haven't been able to break it in this fashion either.
  • '-kbar.ksh'
  • '-k bar.ksh'
  • ' -kbar.ksh'
  • '\-kbar.ksh'
  • 'bar.ksh -k'

Hopefully OP won't hold us in suspense. Curious to see what the cause is.
 
Old 02-21-2012, 04:33 PM   #9
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Quote:
Originally Posted by anomie View Post
Hopefully OP won't hold us in suspense. Curious to see what the cause is.
Haha, agreed.
 
Old 02-21-2012, 04:47 PM   #10
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
thanks to both of your comment, greatly appricated.....below is what I did per your advise:

[linux]/foo=$(basename $0)
+ basename -ksh
basename: invalid option -- k
Try `basename --help' for more information.
+ foo=
[linux]/
[linux]/exit 0
+ exit 0
+ echo logout
logout
$






below is the exactly what I was trying to do:


#!/bin/ksh
usage()
{
print "Usage:
$0 [-u <User>] blah
.....
.....
.....
exit ${EXIT1};
}

MYSP=`basename $0`
 
Old 02-21-2012, 04:54 PM   #11
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
and what is the EXACT command line you are calling it?
 
Old 02-21-2012, 05:02 PM   #12
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Aha!! I've found a way to reproduce the problem.

From the parent shell:
Code:
exec -a \-ksh ksh
Now in ksh:
Code:
basename $0
basename: invalid option -- 'k'
So, something is messed up with your ksh.

However, you can work around the issue the same way.

From your current ksh session, run:

Code:
exec -a ksh ksh
Alternately, you can change your basename call to:

Code:
basename -- $0

Last edited by jhwilliams; 02-21-2012 at 05:05 PM.
 
Old 02-21-2012, 07:19 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Actually, what he's doing is just issuing
Code:
basename $0
from the cli prompt, eg
Code:
[myprompt]# echo $0
-bash
[myprompt]# basename $0
basename: invalid option -- 'b'
Try `basename --help' for more information.
The same is true for eg a ksh env.
 
Old 02-21-2012, 10:54 PM   #14
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
@chrism01: Yes, I guess you're right. In gnome-terminal, I see:

Code:
jameson@yellow:~$ echo $0
bash
However, in a real tty I do get the leading '-' char (-bash).

Last edited by jhwilliams; 02-21-2012 at 10:58 PM.
 
Old 02-21-2012, 11:17 PM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The logon process prefixes the name of the user's shell with "-" when it starts (execs) the shell on successful logon. Shells started in terminal emulators from the GUI desktop environment are not logon shells so simply have the name of the shell without the "-" prefix.

In bash, a robust way to do what brian00 was trying to do in the OP is
Code:
MYSP=${0#-}    # Remove any leading "-"
MYSP=$( basename "$MYSP" )
The basename command can be done using bash parameter expansion:
Code:
MYSP=$MYSP##*/
BTW, a common convention is to reserve uppercase names for environment variables and variables set by bash itself.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] basename with command substitions and xargs dugan Programming 3 06-21-2011 10:58 AM
“basename $0” returns basename: -k: unknown option jaideepca Linux - Newbie 6 11-09-2009 06:30 AM
How to get the 'basename' (or what other.?) command to do what I want..? craftybytes Programming 1 02-13-2008 07:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:33 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration