Results 1 to 7 of 7
Hello
I am trying to understand what this script is doing and I understand most of it. Everything except the last two statements. I have searched the web for similar ...
- 09-08-2008 #1Just Joined!
- Join Date
- Jul 2008
- Posts
- 11
Bash rc.status script
Hello
I am trying to understand what this script is doing and I understand most of it. Everything except the last two statements. I have searched the web for similar patterns with an explanation but have had no luck
if test -z "$LINES" -o -z "$COLUMNS" ; then
eval `exec 3<&1; stty size <&3 2>/dev/null | (read L C; \
echo LINES=${L:-24} COLUMNS=${C:-80})`
fi
test $LINES -eq 0 && LINES=24
test $COLUMNS -eq 0 && COLUMNS=80
How would these lines be translated into human language?
- 09-08-2008 #2Just Joined!
- Join Date
- Jul 2008
- Posts
- 11
Bash rc.status script
I am a novice seeking to expand my point of view so to understand the subtleties not explained or dealt with in the Bash documentation. Right now I am going through the Suse "rc.status" file and the first statement seems odd from my point of view.
I am pretty sure the aspect ratio of a terminal is important, if you are writing a script on a terminal with an aspect ratio of 24:80 you might want to be sure it is run on a terminal with the same ratio.
The purpose of the if/then statement in the file is to ensure that the default terminal has a size ratio of 24 lines to 80 columns and not 0:0.
My first thought was why test, why not just set its parameters. Second, this does not check if the terminal is set to anything other than 0:0, it could be set 17:62 not 24:80. Here again are the lines in question:
if test -z "$LINES" -o -z "$COLUMNS" ; then
eval `exec 3<&1; stty size <&3 2>/dev/null | (read L C; \
echo LINES=${L:-24} COLUMNS=${C:-80})`
fi
test $LINES -eq 0 && LINES=24
test $COLUMNS -eq 0 && COLUMNS=80
export LINES COLUMNS
The first portion of the script determines if either of the environmental variables $LINES and $COLUMNS contain a string of zero length.
If either are true, the script continues by evaluating the arguments of the command substitution containing the "exec" command which causes
standout to be redirected to the standard file descriptor 3;
"stty," to be called and augmented with its "size" option and file descriptor 3.
this is piped to a read command which creates the variables $L and $C.
next "echo" is called to change the environmental variables $LINES and $COLUMNS using parameter substitution so that they contain the constants 24 and 80 respectively.
The next two lines of script seem to be a redundancy of the if/then statement; i,e:
"test" is called and augmented with the variable to check if $LINES is equal to "0" and if this test proves true then the variable is changed to 24. The next "test" statement will change $COLUMNS from 0 to 80.
Again, why not just set the parameters? How is this helpful if the default terminal parameters are anything other than 0:0? Another question: when would the terminal have a default ratio of 0:0?
- 09-08-2008 #3Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
"test" is a shell builtin that is used to compare values and status of files, on the same fashion that "if" can be used.
&& is the AND logical operator.
Each of this lines run test to check if a given value is equal to 0. Then the AND is evaluated.
In linux, each command returns a given numeric value when it ends running. A return value of 0 means success, any other value means it failed (the concrete number can be used to make a distinction on the cause of error).
In a logical AND sentence, the result depends on the value of both members. If one of them is false, the result is false, if both are true, the result it true. This can be used in bash to concatenate commands in a particular way.
If the first command succeed (true), then the second needs to be evaluated (and as a collateral effect, it's also run). In other words, the AND (&&) logical operator can be used to run a given command, only (AND only) if the first one ended with success.
If the first command failts, there's no need to evaluate the second member, bacause the result of the AND expresion is already known (false). Hence, the second command would not run.
In this case, this could be translated as follows:
I hope it cleared this up a bit.Code:if [ "$LINES" -eq 0 ] then LINES=24 fi if [ "$COLUMNS" -eq 0 ] then COLUMNS=80 fi
- 09-08-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
You should be using if [ "$whatever" -lt <value> ] instead. -lt stands for "lesser than".
No idea. I guess this depends on the terminal. Most should never use that values unless they use them for a specific purpose.Again, why not just set the parameters? How is this helpful if the default terminal parameters are anything other than 0:0? Another question: when would the terminal have a default ratio of 0:0?
- 09-09-2008 #5Just Joined!
- Join Date
- Jul 2008
- Posts
- 11
Bash rc.status script
Thanks for looking at this.
I just need this clarified a little. In the following
test $LINES -eq 0
is the zero an exit code or the constant contained in the variable?
Thanks again.
- 09-09-2008 #6Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Hi again,
I was thinking that it could cause you some confusion hehe.
Nope, that is not the return code. The zero you see in the command line is a numeric (a constant, as very well you said) value that we compare to $COLUMNS.
If $COLUMNS is equal to the numeric value "0", the test statement will return "0" (but that has nothing to do with the numeric value, just with the fact that the test statement was true, and hence it returns a zero to the shell. Just a coincidence.
If you need more fine-grained explanation, just ask.
While you become more familiar with test, and the logical operators, I would advice you to use if statements, which are a bit larger to write maybe, but are more human readable.
Anyway, the choice is yours
- 09-09-2008 #7Just Joined!
- Join Date
- Jul 2008
- Posts
- 11
Bash rc.status script
Thank you, that cleared it up.


Reply With Quote
