playing linux series shell programming

I guess you might ask me why I have been updated so recently.
Because I am charging!

shell variable

Defining variables

Common variables:

1
2
a="123"
b="test"

Read-only variable:

1
2
a="123"
readonly a

As the name implies, read-only variables cannot change the content, otherwise the following error will be reported.

1
/bin/sh: NAME: This variable is read only.

Using variables

1
2
echo {$ a} One person echo $ a
c=$a

Only when using variables, the variable name needs to be preceded by a $ sign. {} Optional is of course best used.

Delete variable

1
Unset a # Cannot delete read-only variables

shell data structure

string

1
2
str="123"
str='123'
  • Any characters in single quotes will be output as they are, and variables in single quoted strings will be invalid.
  • Single quotes cannot appear in single quoted characters (not applicable to single quotes after using escape characters)
  • There can be variables in double quotes
  • Escape characters can appear in double quotes
Stitching string and string variables
1
Str_new="this is "$str""

or

1
Str _new="this is {$str}"

Get the length of the string
1
2
string="abcd"
Echo ${#string} #output 4
String Slice
1
2
string="this is a test"
Echo ${string:1:4} #output test

Array

Bash supports one-dimensional arrays (multidimensional arrays are not supported) and does not limit the size of the array.

Defining an array
1
a=(1 2 3 4) # Note is separated by spaces instead of commas

or

1
2
3
4
a[0]=1
a[1]=2
a[2]=3
a[3]=4

Reading an array
1
2
Valuen=${array_name[n]} # read the element of the specified subscript
Echo ${array_name[@]} # read all elements
The length of the array

Get the number of array elements

1
length=${#array_name[@]}

or

1
length=${#array_name[*]}

Get the length of a single element of the array

1
lengthn=${#array_name[n]}

shell input and output redirection

echo

1
2
3
4
Line feed: echo -e "OK! \n" #-e Open escaping
No line break: echo -e "OK! \c" #-e Turn on escaping \c Don't wrap
Output variable name: echo '$a' Output $a Use single quotes
Output command execution result: echo `date` uses back quotation marks

printf

1
2
3
4
printf "%-10s %-8s %-4s\n"
printf "%-10s %-8s %-4.2f\n"
printf "%-10s %-8s %-4.2f\n"
printf "%-10s %-8s %-4.2f\n"

%s %c %d %f is a format substitution character %-10s means a width of 10 characters (- indicates left alignment, no indicates right alignment), any character will be displayed in a 10-character wide character. If it is insufficient, it will be filled with spaces automatically. If it is over, the content will be displayed. %-4.2f refers to formatting as a decimal, where .2 means to retain 2 decimal places.

Shell code content

1
2
3
4
#!/bin/bash
echo $0
echo $1
echo $2

Run the script and pass the parameters

1
./shell.sh a b

Output result

1
2
3
./shell.sh
a
b

Special parameters

  • $# Number of arguments passed to the script
  • $ displays all parameters passed to the script as a single string.
    If “$
    “ is enclosed in “”, all parameters are output as “$1 $2 … $n”.
  • $$ The current process ID number of the script run
  • $! ID number of the last process running in the background
  • $@ is the same as $*, but quotes when used and returns each argument in quotes.
    If “$@” is enclosed in “”, all parameters are output as “$1” “$2” … “$n”.
  • $- Shows the current options used by the shell, the same as the set command.
  • $? Displays the exit status of the last command. 0 means no error, any other value indicates an error.

shell function

Basic format

1
2
3
4
5
6
7
8
9
[ function ] funname [()]
{
action;
[return int;]
}

Function Definition

1
2
3
4
5
6
Test()
{
return $a
}

function uses and gets the return value

1
2
Test
Echo $? # $? is the return value of the function

Function Passing

1
2
3
4
5
6
7
8
9
10
#function definition
Test()
{
echo $1
echo $2
echo $3
}
#function use
Test a b c

shell flow control

if conditional statement

if-then-else-fi
1
2
3
4
5
6
7
8
9
if condition
then
command1
command2
...
commandN
else
command
be
if-then-elif-then-else-fi
1
2
3
4
5
6
7
8
9
if condition1
then
command1
elif condition2
then
command2
else
commandN
be

for loop statement

1
2
3
4
5
6
7
for var in item1 item2 ... item
do
command1
command2
...
commandN
done

One sentence for loop shell

1
for i in $(ps -ef | grep python | awk '{print $2}');do kill $i;done

while loop statement

1
2
3
4
5
6
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done

###shell combat

1
2
3
4
5
6
7
8
9
10
11
#! /bin/bash
#shell Comprehensive use
a=`whoami` #Execute command
b=`date`
c="open"
d=`cat test.log | grep $c`
echo $d
echo "user is $a time is $b"

Reference article

http://www.runoob.com/linux/linux-shell.html

Portal

[[Play linux system] Linux intranet penetration] (https://thief.one/2017/08/09/2/)
[[Play linux series] Vim use] (https://thief.one/2017/08/09/1/)
[[Play linux series] Linux basic commands] (https://thief.one/2017/08/08/1/)

本文标题:playing linux series shell programming

文章作者:nmask

发布时间:2017年08月11日 - 16:08

最后更新:2019年08月16日 - 14:08

原始链接:https://thief.one/2017/08/11/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: