[Linux] Some commands

1. Background commands

Ctrl + Z program station running in the background

bg View running programs in the background

fg serial number transferred to the front desk

2. Execute the command in the shell and assign the result to the variable

variable=$(command)

3. String replacement

  • Script replacement

  • vim edit last line command replacement

  • Replace \N with a space or empty
#将字符串\N换成空或空格

    #将\N替换成空
    cat tmp.txt |sed ':jix;N;s/\\N//g;b jix'

    #将\N替换成空格
    cat tmp.txt |sed ':jix;N;s/\\N/ /g;b jix'

#将换行换成空或空格

    #将\N替换成空格

    echo `cat tmp.txt`

    cat tmp.txt | xargs

4. String interception

  • Use the # sign to intercept the characters on the right (if you want to match the end until the last specified character (substring), then you can use it ##)
url="http://c.biancheng.net/index.html"
echo ${url#*:}

The result is //c.biancheng.net/index.html 

  • Use% to intercept the left character (intercept the left * to be written on the right of the specified character) (If you want to match the end until the last specified character (substring), then you can use %%)

url="http://c.biancheng.net/index.html"
echo ${url%/*}  #结果为 http://c.biancheng.net
echo ${url%%/*}  #结果为 http:

*It is a kind of wildcard, which represents a string of any length. *charsThe meaning used in conjunction is: ignore all the characters on the left until it meets chars

5. If condition judgment

6. The shell divides the string according to the specified symbol

[root@znrmdapp1 rmdDataFlow]# cat tmp.sh
#!/bin/bash
string="hello,shell,haha"
array=(${string//,/ })
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
[root@znrmdapp1 rmdDataFlow]# . tmp.sh 
hello
shell
haha
[root@znrmdapp1 rmdDataFlow]# 

6. The shell determines whether the file directory or file exists

Does the directory jar exist in the current directory

if [ -d "jar" ];
then 
    echo "yes"
else
    echo "no"
fi

Whether the file jar.sh exists in the current directory

if [ ! -f "jar.sh" ];
then
    echo "no"
else
    echo "yes"
fi

Determine whether the hdfs directory exists

dir=/test
hadoop fs -test -e $dir 
if [ $? -ne 0 ]; then
   echo "Directory not exists!"
   hadoop fs -mkdir -p $dir
else
   echo "exists"
fi

7. Linux view the open port number of the server

Linux view the open port number of the server

 

Guess you like

Origin blog.csdn.net/qq_44065303/article/details/112553849