shell
查看shell
Section titled “查看shell”echo $SHELL
cat /etc/shellsvim var.sh#!/bin/bash
n1=1echo $n1
str1="hello"echo $str1
arr1=(1 2 3)echo ${arr1[@]}echo ${arr1[0]}
# 打印目录下所有文件file1=$(ls)echo ${file1[@]}
# 打印环境变量echo $ENV
# -:不会赋默认值,=会若不存在会赋默认值echo ${ENV1:-"8080"}echo ${ENV1:="8081"}echo $ENV1echo $RNV2
# 输出脚本名称echo $0# 输出运行脚本时的参数:bash var.sh -h会输出-hecho $1# 输出1echo "参数个数: $#"
# 控制流a=10b=20if [$a -gt $b]; then echo "更大"else echo "更小"fi
for num in 1 2 3 4 5; do echo "this number is $num"done
# 输出目录下所有文件名for file in $(ls); do echo $filedone
n=1while (($n<5)); do echo $n let "n++"done
echo "please enter a number"read aNumcase aNum in 1) echo 'you input 1' ;; 2) echo 'you input 2' ;; *) echo 'invalid number' ;;esac
function foo() { if [$1 -gt $2];then echo "bigger" else echo "smaller" fi}foo 1 2# 运行脚本bash var.sh
# 将运行结果输出到temp.txtbash var.sh > temp.txt
# 将运行结果追加到temp.txtbash var.sh >> temp.txt
# 将shell脚本中的错误信息输出到error.txtbash var.sh 2 >> error.txt
# 将输出和错误信息合并输出到log.txtbash var.sh > log.txt 2>&1