Common Notes of Big Data Shell

shell program

The first line specifies which program to use to compile and execute the script The following two are equivalent

#! /bin/bash 
#! /bin/sh

system variable

  • Location of system environment variables
 /etc/profile
  • Location of system user variables
find -type f -name '.bash_profile' #查找用户环境变量
source .bash_profile #重启用户  环境变量
  • Environment variables are generally represented by uppercase letters

shell variable

  1. Generally, you do not need to use $ when assigning shell variables. You need to use $ when using or outputting. When adding, subtracting, multiplying and dividing, you need to add two layers of parentheses. There should be a $ outside the parentheses. The variables inside the parentheses can not use $. The assignment of variables and the use of variables cannot have spaces.
  2. There are only integers and strings in shell variable types
  3. shell variable expression
expression illustrate
${#string} Calculate the length of $string
${string:position} Extract the string starting at position
${string:position:len} Extract a string of length len from position
#!/bin/sh
a=10
b=20
c="this is a test"
d=$((a+b))
echo $c
echo "a="$a #输出a的值
echo $d
echo ${d} #也可以
echo $((a/b)) #0

echo "这是原始的字符串:"$c
echo '"${#c}" '${#c}
echo '"${c:5}" '${c:5}
echo '"${c:5:2}" '${c:5:2}

#! /bin/sh
echo "hello shell $1 $2 $3"

#$1,$2,$3可以接收外面传递的参数

./test.sh shanda beautiful

. test.sh shanda beautiful  
#输出结果 hello shell shanda beautiful   
#当其中有空格的时候,用双引号括起来  

$?: Indicates the exit status of the last command before
$$: The current process number of the script running
$0: Indicates the current script name
$1-$9: Indicates the first to ninth positional parameters
$10: Indicates the 10th positional parameter
$#: Indicates the number of positional parameters
$!: The last process number of the script running

exit code

exit N exit status 0 means no error

exit N exit status greater than 0 indicates an error

array

arr=(math english  chinese)
#引用变量:${arr[0]}
#数组个数:${#arr[*]}
#所有元素:${arr[*]}
#数组赋值:arr[0]=hello

judge

It should be noted that when using [], there must be spaces between each variable and the left and right brackets must also have spaces.

test -e filename|[-e filename]:表示文件是否存在
test -d filename 判断文件是否是文件夹
test -f filename 判断文件是否是文件
test -r filename 判断是否有可读权限
test -w filename 判断是否有可写权限
test -x filename 判断是否有可执行权限
test n1 -eq n2 && echo "n1==n2" || echo "n1!=n2"判断n1是否等于n2
test n1 -ne n2 判断n1是否不等于n2
test n1 -gt n2 判断n1是否大于n2
test n1 -lt n2 判断n1是否小于n2
test n1 -ge n2 判断n1是否大于等于n2
test n1 -le n2 判断n1是否小于等于n2
test -z string 判断字符串是否为0 若string是空字符串返回为true  
test -n string 判断字符串是否非为0 若string是空字符串返回为false
test n1=n2 判断n1是否等于n2
或者是[ n1==n2 ]

if judgment

You can use && and || for multiple conditional judgments

if [条件];then
    当条件成立时候执行指令工作雷荣
    
fi表示结束if条件


if [条件];then
    do some things
elif [条件]; then
    do some things
else
    do some things
fi

cycle

#!/bin/sh
for var in 1 2 3 4 5
do 
  echo ${var}
done
    
while 条件
do
 do something
done

The break command is also applicable to shell statements, break out of all loops
break means out of the current loop The default value is 1 means out of the current loop You can also add other parameters to indicate out of a multi-layer loop break 2
continue means to jump out of the current loop The default value is 1

#!/bin/sh
for var in 1 2 3
do
  echo "1"
  for var1 in 3 4 5
    do
     break 2
    echo "2"
    done
done
#结果是 1 跳出了外层的循环
for var in 1 2 3
do
  echo "1"
  for var1 in 3 4 5
    do
     break 1
    echo "2"
    done
done
# 结果是 1 1 1

shell function

The return value of the shell function can only be an integer, which is generally used to indicate whether the execution of the function is successful or not. 0 indicates success, and other values ​​indicate failure. If the return value returns a string, an error will occur.

#!/bin/sh
printStr()
{
  echo "hello"
  return "fsdf"
}

printStr
#输出结果
#hello
#bash: return: fsdf: numeric argument required

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325317224&siteId=291194637