Operating system Linux EX2 Note

Source: Internet
Author: User
Tags chmod

Locate filename Search File

Prints the list of files in the current user directory to the file List1.txt (under the current user directory),
Ls-l > List1.txt

Use the pipeline command to output the file list of all modified dates under the root (/) to List2.txt (current user directory) on April 24.
LS/L
LS/l | grep ' April ' > List2.txt

Merges the file list1.txt with the file list2.txt and outputs it to list.txt.
Cat List1.txt list2.txt > List.txt

Use the Nano to write the Linux script sum.sh, which functions as follows:
Request user input positive integer number n
Determine the value of N, if n is less than or equal to 0, the output error message
< current time > Input error! n = < n value of input >
Append to Error.log and exit the script with the error code number 5th.
If the input is valid, all integers from 1 to n are computed and output to SUM.R.


Execution mode
(1) Bash script-name or sh script-name
(2) Path/script-name or./script-name

Attention:
(1) In the shell, if the first letter of a line is #, then it is a comment, but the above two are written on the first line, so it is not a script comment line, and if it is written after a command, it becomes a comment line.
(2) SH is a soft link for bash, in most cases, the beginning of the script using "#!/bin/bash" and "#!/bin/sh" is no different, but the more canonical notation is to use "#!/bin/bash" at the beginning of the script.

#!/bin/bash notes
Read-p "input" N input
echo "N is $n" >>./out.txt output to File
echo "N is $n" output to console

------------------------------------------------------------------------------
Two commands run together, lose on the same line, with; separate. Maximum no more than 255 characters
Date display time
Who shows who is currently logged on to the system

Script First line #!/bin/bash/
In addition to the first character of the first line, you can use # to indicate a comment

Run
./test1
No permissions
When you create a file, the Umask value is the default permission setting of 022, and the system creates the file with read-only and write-only permissions
Modify permissions with chmod u+x test1
chmod 777 A
./A
This method shell storage file A does not have a suffix name

displaying messages
echo OK without quotation marks when the string does not contain quotation marks
Echo ' OK '
echo "OK" available in double quotes, single quotes
With the above will be automatically wrapped, if not wrapped, plus parameter-n
Echo-n "OK"
You need to enclose the string in quotation marks, and make sure that there is a space at the end of the string (as if not? )

Using variables

Environment variables
$USER
$UID
$HOME
Uppercase only

User variables
No more than 20 characters, case sensitive
Spaces are not allowed between variables, equals, and variable values
By the $ reference
Assigned value does not need $, the assignment needs
A= $b

Anti-Quote
Allows the output of the entire command to be assigned to a variable
testing= ' Date '
Capture the current date with an anti-quote and use it to create a unique file name in the script
#!/bin/bash
today= ' Data +%y%m%d ' #加号左边必须有空格, not on the right. Use anti-quote
Ls/bin-al > Log.%today

Output redirection
Date > Test.txt If the file exists, overwrite
Date >> Test.txt Attach

Input redirect
Command < Inputfile
WC command
WC < TEST6
3 values generated by default
Lines of text
Number of text words
' Number of text bytes

Sort command

Mathematical calculations
Aa=4
bb= ' expr $aa + $aa 0 ' #反引号
echo "$BB"

Use parentheses
Var1=$[1 + 5]

bash shell only supports integer algorithms
About floating point decimals P142, 143

Exit script
Exit status 0~~255, Success 0
Exit Status Table P145
The default is to exit with the exit status of the last command of the shell script
Can be changed to its own exit status code
Exit 5
Exit $var 3

If-then statements
The IF Statement command returns an exit state of 0, which executes the statement after then, not 0, skipping.

if command
Then
Commends
Fi

If date
Then
echo "It worked"
Fi

if command; then# add a semicolon at the end of command
Commands
Fi

If-then-else statements
if command
Then
Commands
Else
Commands
Fi

Elif
if command
Then
Commands
elif command
Then
Commands
Fi

Test command
if test condition
Then
Commands
Fi

if [condition]
Then
Conditions
Fi
Square brackets before and after the space to be added

Numerical comparison
N1-eq n1==
-ge>=
-gt>
-le<=
-lt<
-ne!=
Note that the reference variable in [] requires $

string comparison
STR1 = str2
Str1! = str2
STR1 < STR2
str1 > STR2
-N str1 Check if the length of the str1 is greater than 0
-Z str1 Check if the length of the str1 is 0

if [$USER = $testuser]
If [$val 1 > $val 2]
If [$val 1 \> $val 2]
If [-N $val]
If [-Z $val]

File comparison P155
-d-e-f ..........

Compound condition Check
[Condition_1] && [condition_2]
[Condition_1] | | [Condition_2]

If then advanced features
Double parenthesis P162
((expression))
((val2= $val 1 * * 2)) #取幂

Both brackets P162
[[Expression]] provides pattern matching
Regular Expressions ...

casep163


1. Use the nano to write the Linux script sum.sh, which functions as follows:
Request user input positive integer number n
Determine the value of N, if n is less than or equal to 0, the output error message
< current time > Input error! n = < n value of input >
Append to Error.log and exit the script with the error code number 5th.
If the input is valid, all integers from 1 to n are computed and output to SUM.R.

#!/bin/bash

Touch Error.log
Touch SUM.R

Read-p "Please enter a positive integer:" n
If ["$n"-le 0]
Then
echo "$ (date) Input error! n= $n ">> error.log
Exit 5
Else
Sum=0
While ["$n"-GT 0]
Do
sum=$[$sum + $n]
n=$[$n-1]
Done
echo "from 1 to N: $sum" >> SUM.R
Fi


Run with bash sum.sh
Use echo $? To get the value returned by exit

Operating system Linux EX2 Note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.