LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2007, 08:26 AM   #1
Chong
LQ Newbie
 
Registered: Jan 2007
Posts: 3

Rep: Reputation: 0
Unix vs. Linux shell command


I have a shell script written in Unix using command:

set -A envvar1 $envvar2
typeset -l variable1

What is the equivalent command for set -A and typeset -l in Linux?

Thanks
 
Old 01-09-2007, 08:45 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,370

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
Hi

It's not really Unix versus Linux, it's about what kind of shell. Under linux you can use lots of different shells, and some scripts are not portable, meaning they will only run with shell "bash" or "ksh" or whatever.

The "typeset" looks like its from "Korn Shell".

The first line of the script should tell you what kind of shell it should run with, but a lot of people use "sh" here meaing it should use default shell, even if the script is not portable and requires a certain shell.

If you look in your distro with a package manager or something, you'll probably see lots of shells you can install. I would install Korn Shell and then run it by typing "ksh" and then just execute the script.
 
Old 01-09-2007, 10:26 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594
The "typeset" looks like its from "Korn Shell".
Code:
]$ echo ${BASH_VERSION:0:4}
2.05
]$ help typeset
typeset: typeset [-afFirtx] [-p] name[=value] ...
    Obsolete.  See `declare'.
So Bash *can* use it :-]

For "set -A" in Bash you can just define the array: envvar1=(${envvar2}).
For "typeset -l" in Bash there is no equivalent IIRC, so maybe: var=`eval echo $var|tr [A-Z] [a-z]`?


a lot of people use "sh" here meaing it should use default shell, even if the script is not portable and requires a certain shell.
...in which case one could have the script re-exec using the "right" shell? Taken from Rootkit Hunter:
Code:
     31 if [ "${OPERATING_SYSTEM}" = "SunOS" ]; then
     32         if [ -z "$RANDOM" ]; then
     33                 if [ -n "`which bash 2>/dev/null | grep '^/'`" ]; then
     34                         exec bash $0 $*
     35                 else
     36                         exec ksh $0 $*.
 
Old 01-09-2007, 12:38 PM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,370

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
Neat

I guessed it was ksh because it looked like it, and I think ksh is more used on Unix.

But writing scripts that will work with any shell must be hard, no? I never tried - just used a prefix - usually bash.

I guess you just have to follow some POSIX - good goal, but very hard. For a discussion on this, check out:
https://launchpad.net/ubuntu/+source/dash/+bug/61463

I'm lazy and just prefix scripts with bash
 
Old 01-10-2007, 12:11 AM   #5
Chong
LQ Newbie
 
Registered: Jan 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn
The "typeset" looks like its from "Korn Shell".
Code:
]$ echo ${BASH_VERSION:0:4}
2.05
]$ help typeset
typeset: typeset [-afFirtx] [-p] name[=value] ...
    Obsolete.  See `declare'.
So Bash *can* use it :-]

For "set -A" in Bash you can just define the array: envvar1=(${envvar2}).
For "typeset -l" in Bash there is no equivalent IIRC, so maybe: var=`eval echo $var|tr [A-Z] [a-z]`?


a lot of people use "sh" here meaing it should use default shell, even if the script is not portable and requires a certain shell.
...in which case one could have the script re-exec using the "right" shell? Taken from Rootkit Hunter:
Code:
     31 if [ "${OPERATING_SYSTEM}" = "SunOS" ]; then
     32         if [ -z "$RANDOM" ]; then
     33                 if [ -n "`which bash 2>/dev/null | grep '^/'`" ]; then
     34                         exec bash $0 $*
     35                 else
     36                         exec ksh $0 $*.

Thanks for the information. Yes, I'm using ksh in Unix now. When I run the script, it throws set -A (option not available), hence I thought this command is more related to Linux.

Can you explain in detail how can I replace set -A. The statement look like below: (lineArray & infoLine is a defined variable).
set -A lineArray $infoLine
 
Old 01-10-2007, 03:58 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You are still confusing the O/S and the shell. The fact a shell script is designed to work with bash doesn't imply it runs only on Linux. You just need to specify it's a bash shell when on Unix, assuming bash is installed on the target Unix platform, which is commonly the case nowadays.
 
Old 01-10-2007, 06:29 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,843

Rep: Reputation: 2572Reputation: 2572Reputation: 2572Reputation: 2572Reputation: 2572Reputation: 2572Reputation: 2572Reputation: 2572Reputation: 2572Reputation: 2572Reputation: 2572
IOW, a shell is just an interface to the OS, and Unix/Linux usually has several different shells avail for use
see /etc/SHELLS (I think, I'm writing this from MS ... don't ask).
Of course, in the comercial env, your local sysadmin may dictate that only a given shell may be used for consistency, & he'll prob pick his favourite, which may be a csh derivation, which are significantly different to ksh/bash style shells.
 
Old 01-12-2007, 12:51 AM   #8
Chong
LQ Newbie
 
Registered: Jan 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks all for the information.

I'd never use Linux platform b4, hence a bit confused.

Correct me if I'm wrong, if those script written in ksh, i just have to change it to bash shell or ksh actually work in Linux platform too?
 
Old 01-12-2007, 02:07 AM   #9
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by Chong
Correct me if I'm wrong, if those script written in ksh, i just have to change it to bash shell or ksh actually work in Linux platform too?
There is a public domain version of ksh which is included in many linux distros, called pdksh (public domain ksh, surprise surprise). Note that ksh and bash are both derivatives of 'sh', also known as the bourne shell; as such, they share most of their syntax and both are backward compatible with sh.

For what its worth, csh and tcsh are not derivatives of sh; wars have been fought about the use of csh vs. sh as a better scripting language. sh and its derivatives won that war.
 
Old 01-12-2007, 02:26 AM   #10
jerril
Member
 
Registered: Nov 2005
Location: Ontario, Canada
Distribution: Linux Mint
Posts: 116

Rep: Reputation: 16
If you want to ensure the highest level of portability, it is advisable to use sh. The original Bourne shell not the newer BASH (Bourne Again SHell). All Unix and Linux systems should have it.

I consider it good policy to write my scripts with sh, even though I prefer BASH interactively.

jer

Last edited by jerril; 01-12-2007 at 02:27 AM.
 
Old 01-12-2007, 08:41 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
If you want to ensure the best level of portability, it is more advisable to use only shell constructions compatible with the POSIX standard http://www.opengroup.org/onlinepubs/...cu_chap02.html , and so avoid any other particular shell extensions.

Then your shell script will run under most if not all Unix and Unix like platforms, which all provide a standard compliant shell.
 
  


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
UNIX shell script: split long command on multiple lines loopoo Linux - Newbie 2 10-23-2006 09:34 AM
Problem with shell script using a variable from a unix command. abefroman Programming 1 05-10-2006 02:14 PM
How The Unix OS translate the shell command flavor Programming 4 10-25-2005 12:02 PM
Can anybody really help me to create my own shell for linux or unix. sri_hassan Programming 2 10-10-2005 07:57 AM
unix command to linux command leonidas Linux - General 1 09-10-2004 12:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:39 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