Linux常见命令汇总

1. If 条件语句

if is a command in Linux which is used to execute commands based on conditions

if COMMANDS;
then COMMANDS;
[ elif COMMANDS; then COMMANDS; ]… [ else COMMANDS; ]
fi

ServiceFlag=$(netstat -anp | grep 7080)
#返回是string, string和string一起还是string,这样写可以避免一些问题
if [ x"$ServiceFlag" = x ]; 
then 
	echo "Error !!!!!! Service failed, please check!!!!!!!!!!!"; 
else 
	echo "SERVICE starts successfully"; 
	echo $ServiceFlag; 
fi

2. while 语句 + sleep 语句

command1 to command3 will be executed repeatedly till condition is true

while [ condition ]
do
command1
command2
command3
done

#!/bin/bash
x=1
while [ $x -le 5 ]
do
  echo "Welcome $x times"
  #两层括号里面的变量就会当数字,一层括号里面的数字仅仅当成string
  x=$(( $x + 1 ))
done

while + sleep 可以当作定时器使用


while true
  do
    Date=`date +%Y%m%d --date="1 day ago"`
    find . | grep $DATE
    sleep 604800
  done

3. grep 语句

The grep command which stands for “global regular expression print.

grep -i “harry” /etc/passwd

-i : ignore word case

if [ -d '/directory/path/' ]
then
    echo 'ok'
    cd /home/linhui/workspace/OCRService-20181008/src/
    find . |grep -v score|grep core|xargs rm -rf
fi

4. awk 语句

AWK Stands for ‘Aho, Weinberger, and Kernighan‘

Awk is a scripting language which is used for processing or analyzing text files. Or we can say that awk is mainly used for grouping of data based on either a column or field , or on a set of columns. Mainly it’s used for reporting data in a usefull manner.

awk ‘pattern {action}’ input-file > output-file

$ cat  awk_file
Name,Marks,Max Marks
Ram,200,1000
Shyam,500,1000
Ghyansham,1000
Abharam,800,1000
Hari,600,1000
Ram,400,1000

a. print all lines from a file

$ awk{print;}’ awk_file
Name,Marks,Max Marks
Ram,200,1000
Shyam,500,1000
Ghyansham,1000
Abharam,800,1000
Hari,600,1000
Ram,400,1000

b. print only specific field like 2nd and 3rd column

awk -F “,” ‘{print $2, $3;}’ awk_file
Marks Max Marks
200 1000
500 1000
1000
800 1000
600 1000
400 1000

In the above command we have used the option -F “,” which specifies that comma (,) is the field separator in the file

c. print lines which matches certain pattern

$ awk ‘/Hari|Ram/’ awk_file
Ram,200,1000
Hari,600,1000
Ram,400,1000


d. find and print all unique value in the first column of name

$ awk -F, ‘{a[$1];}END{for (i in a)print i;}’ awk_file
Abharam
Hari
Name
Ghyansham
Ram
Shyam

5. xargs 语句

Xargs is a great command that reads streams of data from standard input, then generates and executes command lines; meaning it can take output of a command and passes it as argument of another command.

a. find out all the .png images and archive them using the tar utility

find Pictures/tecmint/ -name "*.png" -type f -print0 | xargs -0 tar -cvzf images.tar.gz

the action command -print0 enables printing of the full file path on the standard output, followed by a null character and -0 xargs flag effectively deals with space in filenames

b. You wish to know the number of lines/words/characters in each file in the list

ls *upload* | xargs wc

wc - word count; prints a count of newlines, words, and bytes for each input file.

6. killall 语句

killall is a tool for terminating running processes on your system based on name. In contrast, kill terminates processes based on Process ID number (PID). kill and killall can also send specific system signals to processes.

killall [process name]

kill [PID]

how to find running process?

Use a utility like htop or top to view a real time list of process and their consumption of system resources.

Use the ps command to view processes that are currently running and their PIDs.

7. make 语句

make ---- utility for building and maintaining groups of programs

8. exit 语句

The exit command lets you quit the shell where it’s run.

exit: exit [n]
Exit the shell.
Exits the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.

I exited the shell using the exit command and used 8 as the exit status value.

exit 8

In the parent shell (where I returned), I used the following command to check the exit status:

echo $?
8
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值