Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use sh script commands

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use sh script commands", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn how to use sh script commands!

The first line of the #! / bin/bash file indicates that the code behind the computer needs to be interpreted with / bin/bash

Echo output content

Param=$ (cat. / test.txt) variable assignment, parameter name\ equal sign\ value, there can be no space between the three, if you need to get some information obtained by the command as the value, you can use this way, variable = $(command)

The difference between variable assignment single quotation marks and double quotation marks. Single quotation marks do not escape the content, while double quotation marks escape the content.

Variable can be used by using $param or ${param}

Get user input value

Echo "Please enter the value you want to enter" read inputValue echo $inputValue # what is printed here is the value entered by the user param=$inputValue # here the value entered by the user is assigned to param echo $param # what is printed here is also the value entered by the user

The sh subscript passes in the parameter sh. / test.sh a b

#! / bin/bashecho $0 # test file name echo $1 # a the first parameter echo $2 # b the second parameter can continue to write the number of parameters passed to the script echo $* # a b displays all parameters passed to the script in a single string echo $$# current process number echo $! # d the last process number.

Getopts sh script parameter method, such as: test.sh-a 1 create script test.sh

#! / bin/bashwhile getopts "a:w:p:n" arg; # the parameter must be-an arg in a, followed by a colon indicating that the parameter must be passed in the parameter "$arg in a") # all versions are unified parms1= "$OPTARG" W) # Front end version number parms2= "$OPTARG";; p) # php version number parms3= "$OPTARG";; n) # latest version number parms4=1 ?) Echo "unknown parameter:-a;-w;-p;-n" exit esacdone

For example, if you execute sh. / test.sh-a 1, you can select the parameter name-a, then you must pass in the parameter 1, and you can not use the parameter.

Operation of string

Param='abcd_' Param1= "$param" 'efgh' # concatenate the string, echo $param1#abcd_ efghecho ${# param1} # calculate the string length output 9echo ${param1:1:4} # intercept the string from the 1st subscript to the end of the 4th output bcd_ know that the subscript starts at 0 echo ${param1#abcd_} # intercepts the rest of the string from the left match to the right match Note that you must start from the beginning, that is, you cannot write a d _ start match. The output efghecho ${param1#*_} # intercepts the remaining strings from left to right matching * represents arbitrary, similar. * output efghecho ${param1%gh} # intercepts the remaining strings from right to left matching. Note that it must be calculated from the end. That is to say, you can't write a _ e to start matching. The output abcd_efecho ${param1%_*} # intercepts the remaining strings from right to left * represents arbitrary, similar. * output abcd_efecho ${param1:0-4} # matches 2 from right to left, intercepts the input efghecho ${param1:0-4:2} # from right to left, and then intercepts 2 output gh from left to right.

Array

Definition of array= (a b c d) # array echo ${array [*]} or echo ${array [@]} # output array all values a b c decho ${array [0} # a subscript output

The operation of numbers must be wrapped in the symbol "`". This is not a single quotation mark, but a back quote.

Echo `expr 1, 1 `# plus echo `expr 2-1` # minus echo `expr 2\ * 2` # multiplied by echo `expr 4 / 2 `# except echo `expr 5% 2` # take the remainder let a=1+1let bread2-1echo $a $b # let to define one or more expressions

If process control

If []; # condition, pay attention to the space then # execute elif # conditional then # execute else # No fi # end the tag! # non-if [$a! $b]-a # and and if [$a-a $b]-o # or or if [$a-o $b] & & # AND | | comparison of # OR numbers-eq # detects whether two numbers are equal-ne # detects whether two numbers are not equal-gt # detects whether the number on the left is greater than the number on the right-lt # detects whether the number on the left is less than -ge # on the right detects whether the number on the left is greater than or equal to the number on the right-le # detects whether the number on the left is less than or equal to the string on the right = detects whether two strings are equal! = detects whether two strings are not equal-z detects whether the string length is 0-n detects whether the string length is not 0$ detect whether the string is empty folder comparison-e to determine whether the object exists-d to determine whether the object exists And judge the existence of the object for the directory-f, judge the existence of the object for the regular file-L, judge the existence of the object for the symbolic link-h, and judge the existence of the object for the soft link-s, and judge whether the object exists for a length not 0-r, and read-w to determine whether the object exists, and write-x to determine whether the object exists. And executable-O determines whether the object exists, belongs to the current user-G to determine whether the object exists, and belongs to the current user group-nt determines whether file1 is newer than file2 ["/ usr/test1.txt"-nt "/ usr/test2.txt"]-ot determines whether file1 is older than file2 ["/ usr/test1.txt"-ot "/ usr/test2.txt"]

For cycle

Param= (1 2 3 4 5 6 7 8) for i in ${param [*]} # the output behind this in shows do echo $I # 1 2 3 4 5 6 7 8done

While cycle

I=1while ($I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report