LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-04-2006, 05:56 PM   #1
jisner
LQ Newbie
 
Registered: Nov 2006
Location: New Jersey, USA
Distribution: Ubuntu
Posts: 3

Rep: Reputation: 0
Linux equivalent of $SHLVL or $SH_LEVEL


This is my first post. I have some shell scripts that I'm bringing over from Solaris to Linux (Ubuntu). Several of these scripts mention the shell variable $SH_LEVEL and $SHLVL. Both variables are defined in Linux, but both always return 1, rather than indicating the actual shell depth. For example:

echo $SHLVL
1
bash
echo $SHLVL
1


etc.

What is the correct way to determine the shell depth, or is this concept missing in Linux?
 
Old 11-04-2006, 08:00 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
That is very strange - it works as expected for me, and I only see SHLVL:
Code:
matthew@chubby:~$ set |grep -e LVL -e LEV
SHLVL=1
matthew@chubby:~$ bash
matthew@chubby:~$ set |grep -e LVL -e LEV
SHLVL=2
What version of bash / Ubuntu are you using? I'm on edgy running the 386 kernel with bash as follows:
Code:
matthew@chubby:~$ bash --version
GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
 
Old 11-05-2006, 08:53 AM   #3
jisner
LQ Newbie
 
Registered: Nov 2006
Location: New Jersey, USA
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
Ah, thanks. I have to admit that I lied in my original post. I'm actually using ksh, but I thought it didn't matter when I typed the example. With ksh, these variables are always 1. So I guess the problem is with ksh. Versions: ksh (93q+93r-1)on Ubuntu (6.06).
 
Old 11-05-2006, 09:25 AM   #4
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Works fine in zsh too. I just tried ksh (93) with SHLVL currently at 2 and it's still 2, so I guess ksh doesn't use it at all.
 
Old 11-05-2006, 09:29 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by jisner
Ah, thanks. I have to admit that I lied in my original post. I'm actually using ksh, but I thought it didn't matter when I typed the example. With ksh, these variables are always 1. So I guess the problem is with ksh. Versions: ksh (93q+93r-1)on Ubuntu (6.06).
I don't think ksh supports this feature. There's no mention of it in the manual page, and looking though the built-in special variables names, I couldn't fund anything which looks like it does the same thing.
 
Old 11-06-2006, 08:17 AM   #6
jisner
LQ Newbie
 
Registered: Nov 2006
Location: New Jersey, USA
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42
I don't think ksh supports this feature. There's no mention of it in the manual page, and looking though the built-in special variables names, I couldn't fund anything which looks like it does the same thing.
It worked on Solaris, so I think this must have something to do with Linux, or the Linux version of ksh. Oh well, it's not critical. Thanks for your comments!
 
Old 11-06-2006, 09:01 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Perhaps the Solaris ksh is newer, or on that system it has been fudged to use SHLVL.

Here's how to fudge it. In your .profile, add the line:
Code:
export ENV="$HOME/.ksh_shlvl"
Then create the file $HOME/.ksh_shlvl and put this in it:
Code:
if [ "$SHLVL" = "" ]; then
  SHLVL=1
else
  let SHLVL+=1
fi
If you want it system-wide, you can put the ENV in the /etc/profile of course, and have the location of .ksh_shlvl somewhere else, e.g. /etc/ksh_shlvl.

Caveats:

1. The first shell you open has to be a login shell. All subsequent shells will inhert the ENV value as long as you exported it in the first shell. Therefore this might not work in scripts unless you explicitly set the ENV value, but I think all shells you start after re-logging into X (or the console) should have it. I'm assuming you use ksh as your interactive shell here. Be careful about cron jobs - you don't get the /etc/profile or the local .profile executed in cron shells.

2. You can't use your ENV variable for anything else. If you need it for something else, you;ll have to handle the SHLVL thing yourself.

3. Forgetting to copy that weird .ksh_shlvl file to a new system may break your scripts. Fudges like this invariably lead to annoying problems.

4. If someone annoyingly sets SHLVL to a non-integer, you're going to get annoying errors.
 
  


Reply



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
Fineprint equivalent for Linux? montalin Linux - Software 1 11-06-2005 03:23 PM
Tmpeg equivalent for linux? akudewan Linux - Software 2 11-19-2004 10:54 PM
What is the Linux equivalent of ADaware? studpenguin Linux - Newbie 4 03-28-2004 03:27 PM
TurboTax or equivalent for linux? pekuekfir Linux - General 5 09-17-2003 05:07 PM
truss(1) equivalent in Linux ? ashishva Programming 1 06-13-2003 06:46 AM

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

All times are GMT -5. The time now is 09:18 PM.

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
Open Source Consulting | Domain Registration