Part Three Shell Programming 3 (Shell Script 2)

Source: Internet
Author: User

7. If you judge some special usage
If [-Z $a] This indicates what happens when the value of variable A is empty
If Grep-q ' 123 ' 1.txt; Then what happens if the 1.txt contains a ' 123 ' row
if [!-e file]; Then what happens when the file doesn't exist?
if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ... Symbols such as <,>,==,!=,>=,<= cannot be used in []

if [! $a-gt 0];then ... Represents no more than 0

If [-Z $n 1] is empty
If [-n $n 1] is not empty
if [!-Z $n 1]

8. Case judgment in the shell
Format: Case variable name in
value1)
Command
;;
value2)
Command
;;
*)
Commond
;;
Esac


Find more !
Vim case.sh
!#/bin/bash
n1= ' echo $ |sed ' s/[0-9]//g '
if [-Z $n 1]; Then
m=$[$1%2]
Case $m in
1)
echo "The number is odd"
;;
0)
echo "The number is even"
;;
*)
echo " is Other"
;;
Esac
Else
echo "The character you input not a number"
fi


SH case.sh 6 represents the first parameter

9. Loops in shell scripts
For loop syntax structure: The for variable name in condition; Do ... done
While loop syntax structure: while condition; Do ... done dead loop used: denotes
Break directly ends this layer loop (jumps out of the entire loop); Continue ignores the code under continue (jump out of this loop) and proceed directly to the next loop
Exit directly out of the shell

Sequence
SEQ 1 [Step] 10 default is 1
Seq 1 10
Seq 10-1 1
Seq 10-2 1
Seq 1 2 10

Seq-w 1 100 width same ============
001
002
...
100
===============
Vim for.sh
!#/bin/bash
For i in ' SEQ 1 10 '
Do
Echo $i
Done

==========
Vim for.sh
!#/bin/bash
For file in ' ls '
Do
Ls-ld $file
Done

=========
Vim for.sh
!#/bin/bash
Sum=0
For n in ' seq 1 100 '
Do
sum=$[$sum + $n]
Done
Echo $sum

==========while dead Loop
Vim while.sh
!#/bin/bash
n1=1 here can be any value
While [!-Z $n 1]
Do
read-p "Input a number:" N
n1= ' echo $n |sed ' s/[0-9]//g '
Done
echo "The number is $n."
===========
Vim break.sh
!#/bin/bash
For I in 1 2 3 4 5
Do
Echo $i
if [$i-eq 3]
Then
Break
fi
Echo $i
Done


functions in the shell
The function is to organize a piece of code into a small unit, and give the small unit a name, when the code is used to call the name of the small unit directly.
format: function F_name () {
Command
}
the function has to be on the front
You can export global variables in a function
===============
function MySum () {
Sum=$[$1+$2]
Echo $sum
}
s= ' mysum 1 2 '
Echo $s
===============
function MySum () {
Sum=$[$1+$2]
return $sum
}
mysum 1 2
echo $?
=============
function MySum () {
sum=$[$1+$2] If the sum is preceded by a local, it takes effect inside the function

}
MySum 1
Echo $sum
===========

One. Shell Exercises
Write a shell script that calculates the 1-100 and;
Write the shell script, ask to enter a number, and then calculate from 1 to the input number and, if the input number is less than 1, then re-enter until the correct number is entered;

vim sum.sh
!#/bin/bash
Span style= "font-size:15px;" >if_nu () {
n1= ' echo $1|sed ' s/[0-9]//g ' '
If [-Z $n 1]
then
if [$1-GT 1]; then return 1; else return 0;fi
else
return 0
fi
}

Read-p "Input a number:" N
If_nu $n
N2= ' echo $? '
If [$n 2-eq 1]
Then
Sum=0
For i in ' seq 1 $n '
Do
sum=$[$sum + $i]
Done
Echo $sum
Else
echo "The character you input is not number"
Fi

Write shell scripts to copy all directories under the/root/directory (only one level) to the/tmp/directory;
Find/root-maxdepth 1-type D Search the root of the first level directory
===========
For D in ' ls/root/'; do if [-D $d]; Then Cp-r $d/tmp/;fi; Done

For D in ' find/root-maxdepth 1-type d '; do if [-D $d]; Then Cp-r $d/tmp/;fi; Done

Write shell scripts, batch build user user_00, user_01, ... user_100 and all users belong to the Users group;

For i in ' seq-w 0 99 '; Do useradd-g users user_$i; Done

Write a shell script that intercepts the first column in the line containing the keyword ' abc ' in the file Test.log (assuming the delimiter is ":"), then sorts the intercepted numbers (assuming the first column is a number), and then prints out the columns with more than 10 repetitions;

Awk-f: '/abc/{print '} ' test.log |sort |uniq-c |sort-n |awk ' $1>10 '

Part Three Shell Programming 3 (Shell Script 2)

Related Article

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.