Linux shell operation-02 Common commands and cases

Commonly used commands continued

  • scheduled tasks

    • Implement scheduled operations through text editing cron tasks
      • minute hour day month week absolute path sh or cmd
      • * represents each xxx, such as each hour
      • Execute cmd->03 * * * * /home/lauf/scraw.sh at the third minute of every hour
      • Executed at the 5th and 8th hours of every day -> 00 5,8 * * * cmd
      • Hourly execution on Sunday -> 00 */12 * * 0 cmd
      • Every morning -> 00 02 * * * cmd
      • write tasktask.cronin the file.
    • Execute scheduled tasks crontab task.cron
    • Or crontab <<EOF; xxxx; EOF
    • crontab -e directly edits scheduled tasks
    • crontab -l View scheduled tasks
    • crontab -r directly removes all tasks
    • crontab -u operates tasks for a certain user
    • Case
      • 01 * * * * ls -l /home/laufing > log.txt
  • View information

    • hostname, view hostname information; edit hostname vim /etc/hostname
    • cat /etc/hosts to view local address mapping;
    • uname -a, view kernel information
    • cat /proc/cpuinfo cpu information
    • cat /proc/meminfo memory
    • cat /proc/partitions partition
    • cat /proc/version distribution
    • /proc stores process information in memory, and each digital directory is a process;
    • which cmd executable file path; same as whereis, search PATH
    • whatis cmd View function information
    • who is the currently logged in user; whoami is the current terminal user; users; last is the last login information; uptime is the system running time;
  • Process management

    • ps View process information;
    • ps -ef; every process, f field format
    • ps -axf; all processes
    • ps -aux all processes of the current user
    • ps -euf --sort ppid All processes of the current user and sorted according to ppid;
    • ps -ef -o pcpu -o pmem -o comm -o user; Output only the specified columns
    • ps -C xxx; Get information based on process name
      • ps -C mysqld; Get mysql service process information
    • ps -efL; -L also outputs thread information, LWP is the thread id
    • top Check the system resource usage of the process
      • top -o %CPU in descending order by CPU;
      • top -o %MEM Sort descending by memory
    • Linux inter-process communication, used 信号to killdeliver signals to processes
      • kill -l View all signals
        [laufing@centos ~]$ kill -l
      1. SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR111) SIGSEGV12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19 ) SIGSTOP 20) SIGTSTP21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+338) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+843) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+ 1348) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-1253) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-758) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-461) SIGRTMAX-3 62) SIGRTMAX-263) SIGRTMAX-1 64) SIGRTMAX
      • kill -s SIGKILL pid; terminates the process by passing the SIGKILL signal to the process
      • kill -9 pid; force kill process
      • killall is similar to pkill, killing processes based on their name
      • Ctrl + C sends the SIGINT signal to the process;
      • In the case, write a script to keep the process running (event loop), associate the processing function with the SIGINT signal, and print the output each time Ctrl + C is pressed.
        #!/bin/bash
        #handle function
        handler(){ echo “ctrl + c pressed.” } #associate trap “handler” SIGINT while true;do echo running… > log.txt done






 

Simple case

  1. Write a shell script to output whether the current user is the root user.
#!/bin/bash
# 开启调试
set -x

# 数值比较 测试
if [ $UID -eq 0 ]; then
	echo "you are root."
else
	echo "you are common user."
fi

Add executable permissions: chmod u+x lauf.sh

  1. Write a shell script, receive command line parameters m and n, and output mn {m^n}mn
#!/bin/bash
# 变量直接赋值,一切值均为字符串
m=$1
n=$2
echo $m $n
echo -e "calc result:\n" # -e 有转义字符
echo "${m}^${n}" | bc

  1. Write a shell script to create a common array ('tom' 'jack' 'lucy') and use a for loop to iterate through each element.
#!/bin/bash

#!/bin/bash

arr=('tom' 'jack' 'lucy')
for((i=0; i < ${#arr[*]}; i++)){
    
     # 数组长度
    echo ${arr[i]}  # 普通数组,索引访问 arr[@] / arr[*] 获取所有的元素
    echo ${
    
    #arr[i]} # 每个元素的长度
}
  1. Write a shell script to store the price of fruit, apple: 3.4, pear: 2.3, banana: 2.8; pass the name of a fruit on the command line and output its price.
#!/bin/bash

# 声明关联数组,支持字符串索引,相当于python字典
declare -A price
price['apple']=3.4
price['pear']=2.3
price['banana']=2.8

echo "${price[$1]}"

# 命令行执行
$ ./lauf.sh  apple

 
5. Write a shell script to view the five files that occupy the largest disk space in the current directory, and customize a command named lauf. Execute the lauf command on the command line to execute the script.

  • du -ah checks disk usage
  • alias defines a command alias to prevent the input from being too long. It takes effect temporarily in the command line and permanently in the configuration file.
  • Enable debug mode
    • sh -x lauf.sh
    • #!/bin/bash -xv
    • set -x is written in the script statement set +x turns off debugging
#!/bin/bash
du -ah | sort -nrk 1 | head -n 5

# sort 
# -n 数值排序
# -k 指定列
# -r 逆序排序

Insert image description here
6. Write a shell script that requires the user to enter the user name and password. The password is not allowed to be displayed when entering. Finally, the user name and password are printed, and the program execution time is counted.

  • read gets the user's input, such as read password, reads the content and stores it in the password variable;
    read -p "prompt:",
    read -s silently inputs without displaying,
    read -n reads the specified number of characters
  • stty -echo does not allow display; stty echo allows display;
  • date gets the date and time, date +%s gets the timestamp, date +%a week, date +%d date, date +%b month, date +%y/Y year, date +%H hour, date +%M minute , date +%S seconds
#!/bin/bash

# 计算开始时间
start=$(date +%s)   # (xxx) 子shell , 即子进程; $() 相当于``
# 输入用户名
read -p "input your name:" username
# 不显示
stty -echo
read -p "input your password:" password
# 显示
stty echo

# 输出
echo "your name: $username"
echo "your password: $password"
# 计算结束时间
end=$(date +%s)

# 计算差值
let delta=end-start
echo "total time: $delta s."

 
7. Write a shell, loop 10 times, delay 5s each time, and calculate the total program execution time.

#!/bin/bash
start=$(date +%s)
for i in {
    
    1..10}; do
	sleep 5 # 休眠  命令行参数传入时间
	echo $i
done
end=$(date +%s)

delta=$[end-start]
echo "total time: $delta s."

Guess you like

Origin blog.csdn.net/weixin_45228198/article/details/132927877