Quantcast
Viewing all articles
Browse latest Browse all 10

Read command – part 1

To have direct contact with the user, except using the parameters of position, there possibility  to enter data through read command. Data read through  read command channel will be broadcast on standard input and stored in the variables passed as parameters in command read. The syntax is:
read (variable)

At the meeting a read command, the shell expects their data entry and validation by pressing ENTER. The shell splits input line into words, affecting first word of the first variable, the second of the two variables … The last words are saved in the last variable. It is desirable that any read command be preceded by an echo to the user explain what we want to introduce.
Verification of conditions. Test command
Test command to check on compliance of conditions and returns a value to mark it. To meet the test result, use the $?. It has the value 0 if the test is positive (true condition) and a different value of 0 otherwise.
Order test performed on these tests:
• tests on file
• tests on string
• Numerical tests
Tests of files:
Test the file check is to verify if this condition specified by an option. General syntax is:

test option (file or directory)
Possible conditions are:

Options. Significant.
-E file exists (regardless of type or)
-F file normally
-D file directly
Particular file-c (peripheral) type character?
-B block special file
-P file pipe
-R for read access law
-W write access law
-X As execution?
S nonempty file?
Test command can be combined with other orders with operators & & and | |, form-are strings of commands. The combination is used with the echo command:
Doom & & test-x echo “You have the right to execution!”
determines if there is as doom and execution of the file, if so, displays a message.
Tests of string
There are two types of tests on strings: test to check if a string is empty and test to see if two strings are identical. For the first type of test we have two options:
test-z “variable” (test if variable contains an empty string)
test-n “variable (if variable contains a string test nonempty)

Variables are written quotes to ensure that no syntax errors will occur due to empty strings.
The second form allows us to check if two strings are identical or not, for example:
test “$ a” = tax
check if variable $ a is string value tax.
test “$ f”! = exit
check if variable $ f exit no value

This is not compulsory to use quotes for strings.
Numerical tests
For numerical tests, the test command should have three components:
test (value1)-option (value2), where value1 and value2 are terms that compare and order option specifies the relationship used.

MEANING OF OPTIONS
Draw-eq
Unlike us
Less-lt (less than)
Higher-GT (Greater than)
Them more or less
-Ge Greater than or equal
Example:
x = 10
y = 6
test “$ x”-ge “$ y” & & echo “$ x greater than $ y

Script to check if the variable x is greater than or equal to the value of y and
displays the message properly.

If one term is not defined (uninitialized variable), then the test command will not work.

Is it possible to combine several tests by using options combining “logic”:

MEANING OF OPTIONS
! Logical negation
The combination of tests by logical AND Image may be NSFW.
Clik here to view.

A combination of tests by logical OR
Examples:
1) Test! -S “$ f”
returns 0 if the file name containing the variable $ f is empty.
2) test-d “$ f”-a-x “$ f” return 0 if file $ f is an executive director and we are right.
3) test-c “$ 1″-a-b “$ 1″ & & echo “file associated with a peripheral”
returns 0 if the first command line argument is a character type special file or block, in this case and the message is displayed properly.
It is possible to use parentheses to create complex expressions. To use the brackets, they will be preceded by the character \, as in the example below:
test \ (-s “$ 1″ \) the \ (-f “$ 1″-a-d “$ 1″ \)
Exercise: What does it command?
Restricted form of control test
To be more easily used in conjunction with IF statements test command has a simplified form, in which the command name is replaced by square brackets [and]
[-R list]
is equivalent
r test list

Simplified form of execution effect is the same!
IF statement:
The great advantage of the shell programming language control structures is the existence of execution instructions. IF statements allow the execution of an order fulfillment conditioning a logical condition.
Instruction syntax is:
if (logical expression) Image may be NSFW.
Clik here to view.

Then
(Order) …
[Else
(Order) ... ]
be

where:
(Logical expression) – command or series of commands to logical result
(Order) – any order accepted by the shell, including if
IF statement works like similar instructions in languages Pascal and C words IF, Then, as else and are keyword. It is mandatory that IF statement be written as it appears above.
EXAMPLES: 1. The script follows:
if grep “Georgescu” list> / dev / null
Then
echo “The name was found in the list”
else
echo “name is not listed”
be
Georgescu name of the file search list and displays a message stating that the search result. As you know (it?), Grep command without options displays lines that contain the specified pattern. By redirecting output (> / dev / null) is sent to the peripheral null lines of text (referring to “nowhere”), because we care lines found, but only if such lines exist.
2. In this example we rewrite the previous script, template and file where the search is given as arguments on the command line:
if grep “$ 1″ “$ 2″> / dev / null
Then
echo “$ 1 is the file $ 2″
else
echo “$ 1 does not appear in the file $ 2″
be

11


Viewing all articles
Browse latest Browse all 10

Trending Articles