LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   can't get the variable to set (https://www.linuxquestions.org/questions/linux-newbie-8/can%27t-get-the-variable-to-set-4175426629/)

brian00 09-10-2012 10:31 PM

can't get the variable to set
 
doesn't seem to set or know about ADG_svrname env.

suicidaleggroll 09-10-2012 10:40 PM

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.

evo2 09-10-2012 10:44 PM

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.

brian00 09-10-2012 11:14 PM

I am bit confusing of when you use ksh, sh or plain? which is which, could you please explain?

brian00 09-10-2012 11:18 PM

thx

evo2 09-10-2012 11:22 PM

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.

David the H. 09-11-2012 03:09 AM

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

brian00 09-11-2012 07:32 AM

thx

414N 09-11-2012 07:47 AM

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.

David the H. 09-11-2012 09:48 AM

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.

colucix 09-13-2012 03:48 AM

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.

brian00 09-13-2012 08:58 AM

I am sorry, won't do it again


All times are GMT -5. The time now is 12:28 AM.