LinuxQuestions.org
Review your favorite Linux distribution.
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


Closed Thread
  Search this Thread
Old 09-10-2012, 10:31 PM   #1
brian00
Member
 
Registered: Aug 2011
Posts: 33

Rep: Reputation: Disabled
can't get the variable to set


doesn't seem to set or know about ADG_svrname env.

Last edited by brian00; 09-11-2012 at 07:51 AM.
 
Old 09-10-2012, 10:40 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,492

Rep: Reputation: 2093Reputation: 2093Reputation: 2093Reputation: 2093Reputation: 2093Reputation: 2093Reputation: 2093Reputation: 2093Reputation: 2093Reputation: 2093Reputation: 2093
Works for me

Code:
$ cat test
#!/bin/ksh

Sdir=`dirname $0`

ADG_svrname=$Sdir/ADG_svrname.var
echo Writing $ADG_svrname
Code:
$ ./test
Writing ./ADG_svrname.var

$ ~/test
Writing /home/user/ADG_svrname.var

Where are you going with the rest of that if statement? There's no close in your example code.

Last edited by suicidaleggroll; 09-10-2012 at 10:41 PM.
 
Old 09-10-2012, 10:44 PM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

if I add a "fi" to the end of your script it works fine for me.

My ksh is:
Code:
% /bin/ksh --version
  version         sh (AT&T Research) 93u+ 2012-06-28
% dpkg -l ksh
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-==================================
ii  ksh            93u+20120628 amd64        Real, AT&T version of the Korn she
Is there any reason you want to use ksh? What about bash or just plain sh?

Evo2.
 
Old 09-10-2012, 11:14 PM   #4
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
I am bit confusing of when you use ksh, sh or plain? which is which, could you please explain?
 
Old 09-10-2012, 11:18 PM   #5
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
thx

Last edited by brian00; 09-11-2012 at 07:51 AM.
 
Old 09-10-2012, 11:22 PM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

it depends on your needs. For high portability and speed, straight bourne shell (sh) is probably best. For "features" bash is a good option.

IMHO, on a GNU/Linux system ksh is probably not such a good choice these days: bash has a similar feature set and is much more likely to be installed.

There is quite a bit on the web on this topic, and know that others here at LQ have opinions on this.

Cheers,

Evo2.

PS. I use zsh for my login shell, and a either bash or sh for scripting.
 
Old 09-11-2012, 03:09 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Using "sh" means you're executing the script in posix-compatibility mode. It may still be ksh, but restricted in features, or it may be a different shell entirely. You'll almost always want to use the full #!/bin/ksh shebang unless you really need portability.

Also, dirname, and the command substitution that goes with it, is unnecessary, since every major shell has at least basic parameter substitutions built in.

$(..) is highly recommended over `..` too.

QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

And learn how to format effectively too. At the very least indent your subsections.

Scripting With Style

Code:
#!/bin/ksh
if [[ $1 = "start" ]] ; then
	Sdir=${0##*/}
	ADG_svrname=$Sdir/ADG_svrname.var
	echo "Writing $ADG_svrname" >> tlog.txt
fi
#Final note, my links are all to bash sources, but almost all of it applies equally to ksh as well.


Quote:
within ADG_svrname.var, I am more interest of getting the value within this file and set it as a variable.
Show us the contents of the file then. Is it a single line, or multiple, or what?

Again, read is usually the command to use. Assuming there's only a single line:

Code:
read -r variable <filename
For multiple lines you'll probably have to use a while loop.

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001
 
Old 09-11-2012, 07:32 AM   #8
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
thx

Last edited by brian00; 09-11-2012 at 07:50 AM.
 
Old 09-11-2012, 07:47 AM   #9
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
Then you just need to "source" that file, in order to execute every command/variable definition inside there:
Code:
. $Sdir/ADG_svrname.var
or
Code:
source $Sdir/ADG_svrname.var
This way, you'll have MYCURSOR=BLAH defined in your shell environment and you'll be able to use that thereon.
 
Old 09-11-2012, 09:48 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Excuse me, why did you delete your opening post? (And most of the following ones too?) You've now made it harder for readers to follow the rest of the thread. That's considered rather impolite, to say the least. Please don't do it again.

There's nothing to be embarrassed about, if that's your reason. Everybody is new sometime, and we all make mistakes. All criticisms given were only to help you with your problem and to improve your skill.
 
Old 09-13-2012, 03:48 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Moderator note: @brian00: I don't see any reason for deleting your question and subsequent feedback, especially after having received useful answers from other LQ members. This is not respectful towards people that voluntarily spent some of their time to help you! Please, refrain from doing that in the future. Thread closed.
 
Old 09-13-2012, 08:58 AM   #12
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
I am sorry, won't do it again
 
  


Closed Thread


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
How to set the variable elainelaw Linux - Newbie 4 11-02-2009 05:03 AM
How to set Variable Isuru Divarathne Linux - Software 2 10-14-2009 08:42 AM
how to set a variable in a script and have the variable still set after script termin john test Linux - Newbie 9 10-21-2008 05:21 PM
Set variable with value of another Ephracis Programming 7 12-25-2004 10:34 AM
How to set a environment variable in rh? yenonn Linux - Newbie 11 02-21-2003 10:24 PM


All times are GMT -5. The time now is 10:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration