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


Reply
  Search this Thread
Old 04-14-2012, 01:41 PM   #1
tuts73
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Rep: Reputation: Disabled
Cool Bash scripting


Hi all....

I need to diplay the current timout of a shell variable...then edit this to another value. So far my code is

#!/bin/bash

echo timeout is = $TMOUT
TMOUT = 120
echo timeout is = $TMOUT
TMOUT = 0


laptop:~$ bash myscript.sch
timeout is =
myscript.sch: line 4: TMOUT: command not found
timeout is =
myscript.sch: line 6: TMOUT: command not found

Wouls appreciate your help
 
Old 04-14-2012, 01:51 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,873

Rep: Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905
in bash space is not allowed before and after =
so you need to write
TMOUT=120
and
TMOUT=0
without spaces
 
Old 04-14-2012, 02:26 PM   #3
tuts73
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Ah your a star mate - thank you.

alls working now but its not displaying the original timeout value - this is what i get :-
timeout is=
timeout is=120
 
Old 04-14-2012, 02:32 PM   #4
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
read this
about quotes
and this
about how words are recognized by bash

Code:
timeout="$TMOUT"
echo "$timeout"
the difference is, that a "$" sign before a varname makes bash to expand this expression ( the $var) to it's value.
Using the varname without a "$" sign you can assign values.
 
1 members found this post helpful.
Old 04-14-2012, 02:44 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Scripting commands are processed in the order they are encountered. You're echoing the value before you set it, so in the first line TMOUT has a null value, and in the second line it has 120.


BTW, 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


Also, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
 
1 members found this post helpful.
  


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
bash scripting playonmonkey Programming 4 02-17-2012 11:15 AM
bash scripting ZAMO Linux - General 1 12-04-2008 03:53 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
Need help with bash scripting whited Programming 6 10-15-2007 09:54 PM
bash scripting fnoyan Programming 1 01-18-2005 08:35 AM

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

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