Shell script Programming

Source: Internet
Author: User
Tags array definition

Two consecutive! Represents the execution of the previous command!n here the n is the number that represents the nth command in the execution of the command history! String! PW indicates that the most recent command in the command history that begins with PW matches 0 or more characters. Match a character input redirect > >> < 2> 2>>ctrl+z to suspend a task FG command restore BG to drop a task into the background jobs can view task methods that are paused or run in the background: Run the command directly from export path=$ Path:/usr/local/webserver/php/bin and Export path= $PATH:/usr/local/webserver/mysql/bin Use this method to only be valid for the current session. This means that each time the system is logged out or logged off, the PATH setting is invalidated and is only temporarily in effect. Method Two: Execute VI ~/.bash_profile Modify the PATH line in the file, add/usr/local/webserver/php/bin and/usr/local/webserver/mysql/bin to path= $PATH: $ Home/bin after a row this method only for the current logged on user effective method Three: Modify the/etc/profile file to make it permanent, and for all system users, add the following two lines of code at the end of the file path= $PATH:/usr/local/webserver/ Php/bin:/usr/local/webserver/mysql/binexport PATH Last: Executes the command source/etc/profile or the Execute Point command./profile to make its modifications effective, executed through echo $ Path command to see if the add is successful this article originates from the Linux commune website (www.linuxidc.com) original link: http://www.linuxidc.com/Linux/2015-01/111727.htm--------- ------------------------------------------------------------------------------------------------- Shell IntroductionA shell script can only run on Linux platform B Shell is only scripting language, relying on the Linux shell Environment C shell execution efficiency is low, far from C fast D The shell can help us automate operations---------------------------------------------------------------- shell structure and executionVI 1.sh#!/bin/bash# #描述 run: Bash 1.sh sh 1.sh relative path: chmod a+x 1.sh./1.sh absolute Path:/root/shell/1.sh difference See script execution sh-x 1.sh------------ ----------------------------------------------- Date CommandDirect output date and Time Cal view Calendar-y view one year date-s "year-month-day time: minutes: Seconds" Yum install-y ntp sync time ntpdate time.windows.com Microsoft time Server Ntpdate ntp.fudan.edu  . CN Fudan Time Server +%f month Day +%t time +%y four-year +%y two-year +%m month +%d date +%h hour +%m min +%s s +%s distance January 1, 1970 0 o'clock 0 seconds 0 minutes how many seconds has it been? Date + "+%y-%m-%d%h:%m:%s" ----------------------------------------------------------------- Mathematical Operations#!/bin/basha=1b=2sum=$[$a + $b]echo "$a + $b = $sum" #!/bin/basha=1;b=2c=$[$a * $b]; echo $c and user interaction #!/bin/bash# #Using ' read ' in Shell scriptread-p "Please input a number:" Xread-p "please input another numb ER: "ysum= $s [$x + $y]echo" The sum of the numbers is: $sum " The read command interacts with the user------------------------------------------------------ several usages of if judgment1. and document-related judgments-e determine whether a file or directory exists-D is not a directory, and whether the existence of-F is a normal file, and there is a-r document whether the degree of permission-W-----whether there is write permission-X can be executed if [-e filename]; Then 2. Whether the variable is empty-N option can determine whether a variable is empty, note that the variable must be caused,-Z can not be caused by the IF [-N "$a"]; Then echo "A is not NULL"; else echo "A is null"; fiif [-Z $a]; Then echo "A is null"; Fi 3. Determine the condition for a command if Grep-p ' ^aming: '/etc/passwd; Then echo "User aming exist."; The Figrep-p function is to filter but not output--------------------------------------------------------------------------- Case SelectionCase variable invalue 1) command;; Value 2) command;; Value 3) command;; *) command; ESAC does not limit the number of value, * represents a value other than the above. #!/bin/bashread-p "Input a number:" na=$[$n%2]case $a in1) echo "The number is odd";; 0) echo "The number is even."; *) echo "It ' s not a number!";; Esac---------------------------------------------------------------- LoopsFor loop #!/bin/bashfor i in ' seq 1 5 '; DOECHO $idone structure for variable name in loop condition; Docommanddone------------------------------------------------while loop is often used to write dead loops of the script, Used to monitor a service #!/bin/basha=5while [$a-ge 1]; Doecho $aa =$[$a-l]done while condition; Docommanddone to judge the dead cycle of the system load #!/bin/bashwhile:; doload= ' uptime |awk ' {print $ (NF-2)} ' |cut-d.-f1 ' if[$load-gt]thenecho "System load is Hugh." | Mail-s "System Load"[email protected]Fisleep 10done----------------------------------------------------------------------------------------------- Shell interrupt continues to exitBreak directly ends this layer loop #!/bin/bashfor i in ' seq 1 5 ';d oecho $iif [$i = = 3]thenbreakfiecho $idoneecho aaaaaa Continue ignoring continue Code, proceed directly to the next loop #!/bin/bashfor i in ' seq 1 5 ';d oecho $iif [$i = = 3]thencontinuefiecho $idoneecho $i exit Direct Exit Shell#!/bin/bash For i in ' SEQ 1 5 ';d oecho $iif [$i = = 3]thenexitfiecho $idoneecho aaaaaaaa---------------------------------------------- ----- Shell FunctionsThe function is to organize a small piece of code into a small unit, and give the small unit a name, when using the paragraph code, directly call this small unit can format function f_ () {command} function must be placed on the front vim func 1.sh#!/bin/ Bashinput () {echo $1}input AAA addition operation function #!/bin/bashsum () {S=$[$1+$2]echo $s}sum 1 2 #!/bin/baship () {ifconfig |grep-a1 "$" |tail-1 |awk ' {print $} ' |awk-f ': ' {print $} '}read-p ' Pleasr input the ETH name: ' emyip= ' IP $e ' echo ' $e address is $ Myip "---------------------------------------------------------------------- Shell ArrayArray definition, a pair of parentheses represents an array, the array element is separated by a "space" symbol a= (1 2 3 4 5) Echo $a 1 Gets the number of elements in the array echo ${#a [@]} reads an element from the array, the number of marks starts at 0, A[0] is the first element echo ${a[2]} Prints the entire array of elements, or uses echo ${a[@]}echo ${a[*]} array assignment A[1]=100echo ${a[*]}a[5]=100echo ${a[*]} array Delete a= (1 2 3 4 5) unset A[1]echo ${a[ *]}1 3 4 5 array shard a= (' seq 1 5 ') echo ${a[@]:0:3}123 array Replacement a= (1 2 3 4 5) echo ${a[@]/3/100}1 2 4 5--------------------------- ------------ If logic judgment#!/bin/bashread-p "Please input your score:" AIF (A&LT;60), Thenecho "You didn ' t pass the exam." Elseecho "good! You passed the exam. " Fi------------------------------------------------------------------------------------------------------- With elif format: if judgment statement one; Thencommandelif judgment statement two; Thencommandelsecommandfi for example: #!/bin/bashread-p "Please input your score:" AIF ( A&LT;60) Thenecho "You didn ' t pass the Exam" elif ((a>=60)) && ((a<85)); Thenecho "good! You pass the exam "Elseecho" very good! Your Socre is very high! " Fi--------------------------------------------------------------------------------------------------------if LS /DEV/SDB1 >/dev/null 2>/dev/null; Then determine the drive letter

Shell script Programming

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.