The linux shell script (b)

Others say not all wrong, learn to reflect on


argv[]


today = 'date% Y% m % d'
the current date is stored into the variable today.

"Expressions" section later if sufficient space must be separated before they can fall apart too.

#!/bin/bash
if [ "$1" = "me" ]; then
    echo "You are dead..."
else
    echo "$1 is dead...."
fi

Note that this script "[" symbol in front and behind, "$ 1" behind the "=" behind "]" in front,
must have spaces. Otherwise, a script error.


case

Similar to the switch C language, case statements,


! # / bin / bash
echo "the INPUT A Number The:"
the Read NUM
case $ NUM in
One) echo "I know that this a" double semicolon ;; # indicates that this condition case ending
two) echo "I know that the two" ;; 
Three) echo "this I have to think about it ....." # each case can have multiple statements
       echo "Oh, think of it, this is the three" ;; # last statement in a case sure to double semicolon. And only the last statement has a double semicolon
*) echo "This does not recognize the" use ;; # *) represent other values, similar to the default
esac # indicates the end

*) Line may not exist, the match will not perform any commands not see any strings.


Shell in the loop

#!/bin/bash
for num in 1 2 3 4 5 six
do 
echo "num = $num"
done

num = 1
n m = 2
n = 3
n = 4 I
n I = 5
num = six

#!/bin/bash
for (( num = 1; num<7; num++))
do 
    echo "num = $num"
done

Whether it is a while loop or for loop, you can use the break and continue instructions.
Wherein the instructions for break out of this loop, performing following operations; Continue to ignore this instruction cycle, directly back to the loop
start position, perform the next cycle.


Owner of the file, group, others.

---- chmod command to set permissions

chmod [target set permissions] + [permission] [file]

u on behalf of the owner of the file;
the group g represents the file;
O on behalf of other

r stands for read
w represents the write
x represents execution

chmod u + x ./albert.odt
on behalf of u to be the owner for permission to operate;
+ to increase the number of representatives of authority;
x represents an increase is to execute permissions.

I want to get rid of family members in the main read permission on this file,
chmod GR ./albert.odt


Regular Expressions

Regular expressions (Regular Expression) is used to describe or refer to a series of line matching a syntax rules
single string string.

6.4.2 acquaintance regular expressions

grep <string> <file name>
in the "file name" specified by the file, find the row with the "character string" designated in the content, and outputs it to standard output.

grep "sed" ./learn.sh > ./sed_command.txt

">" Can be output to the original content standard output (ie, to print content on the screen), turned into a file.
And if the file already exists, it will overwrite the original file contents. If you do not want to overwrite the file of the original content, you can use the
">>" symbol, instead of ">", so the new content will be added in the file is not the end.

Use "\ b" meta character, this character represents the beginning or end of a word. So what you are looking for, they should say so:
grep "\ BSED \ b" ./learn.sh> ./sed_command.txt
such as kissed, this word used, it will not be matched up. Here, we write "\ bsed \ b" is a regular
expression.


Meta character described
. Matches any character (except newline)
\ W matching letters or characters or numbers, or underscores
\ s matches any whitespace
\ d match numbers
beginning or end \ b matching words
^ Matches the
$ matches the end of the line


grep "\ d \ d \ d \ d \ d \ d \ d \ d" diary.txt
Find diary.txt file, all appeared in eight consecutive digits lines.


Code Description
\ W match any instead of letters, numbers, underscores, kanji character
\ S matches any not whitespace characters
\ D matches any non-numeric characters
begin with \ B matches are not words or end position
[^ x] matches any not x characters
[^ xyz] not match any character x and z is not y and not

  

grep  "\D\d\d\d\d\d\d\d\d\D"  diary.txt

Find diary.txt file, all appeared in eight consecutive digits, and this character before and after an 8-digit numbers are not rows. This will more accurately position
a fixed telephone number.

In fact, here you can streamline it, written like this:
grep "\ D \ d {8} \ D" diary.txt

Common repeat

Code Description
* repeated zero or more times
+ repeated one or more times
? Repeated zero or one time
{n} n times or more times
{n,} n times or more times
{n, m} is repeated n times to m

In the Linux system, as long as you can find a place with regular expressions to streamline operations, improve efficiency,
then this place will certainly support regular expressions.

Guess you like

Origin blog.csdn.net/baiyibin0530/article/details/93063688