shell基础

shell基础

双引号 (""):允许变量展开和命令替换,但保留大部分特殊字符的字面含义。

$  ``  \  !

单引号 (''):所有内容都被视为字面量,不会进行变量展开或命令替换。例如,echo '$VAR' 会输出 $VAR 而不是变量的值。

反引号 ( ):用于命令替换,将命令的输出替换到命令行中。例如,DATE=$(date) 会将当前日期赋值给变量 DATE

command1 && command2 当command1执行成功,则执行command2
command1 ; command2 在一个命令中执行两条命令
command1 || command2 当command1执行失败,则执行command2

shell通配符

\*:匹配零个或多个字符。例如,*.txt 匹配所有以 .txt 结尾的文件。

?:匹配单个字符。例如,file?.txt 匹配 file1.txtfileA.txt 等,但不匹配 file10.txt

[...]:匹配括号内的任意一个字符。例如,file[1-3].txt 匹配 file1.txtfile2.txtfile3.txt

{...}:用于生成一组可能的字符串。例如,{a,b,c}.txt 匹配 a.txtb.txtc.txt

!:在方括号中用来表示“不匹配”。例如,file[!1-3].txt 匹配任何不以 123 开头的文件。


() 引用命令

[root@wsa ~]# echo $(hostname)
wsa

[] 引用运算符

[root@wsa ~]# echo $[a+1]
2

[root@wsa ~]# echo $[(a+1)/2]
1

{} 引用变量

[root@wsa ~]# echo ${a}
1

环境变量和本地变量

本地变量

仅在当前shell中生效的变量

[root@wsa ~]# echo $a
1                                 当前shell可以看到
[root@wsa ~]# bash
[root@wsa ~]# echo $a             另外一个shell无法引用

环境变量

在所有子shell中有效的变量(在另外终端引用不了)

将本地变量转化为环境变量

将本地变量转化为环境变量   export a
定义环境变量     export a=1

[root@wsa ~]# export a
[root@wsa ~]# bash
[root@wsa ~]# echo $a
1

查看变量

set查看所有变量

env查看所有环境变量

[root@wsa ~]# aaa=1000           本地变量
[root@wsa ~]# set |grep aaa       
aaa=1000
[root@wsa ~]# env |grep aaa       set可以查到 env查不到
[root@wsa ~]# export aaa
[root@wsa ~]# env |grep aaa        转化为环境变量之后env可以查到
aaa=1000

全局环境变量

[root@wsa ~]# cat /etc/profile  定义在该文件中的变量。所有用户均能获取到
city=wuhan
export city

[root@wsa ~]# source /etc/profile 读取此文件中变量信息

[root@wsa ~]# env |grep wuhan
city=wuhan
[root@wsa ~]# set |grep wuhan
city=wuhan                         在此shell和新开窗口中都能读取到此变量


用户环境变量

[root@wsa ~]# cat .bash_profile   用户环境变量   登录用户时先读全局环境变量,再读用户环境变量
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then       
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

登录shell和非登录shell

登录shell

su - user

/etc/profile
/etc/bashrc 
.bashrc
.bash_profile 

非登录shell

打开新终端
su user 切换用户
执行脚本

/etc/bashrc 
.bashrc

shell中的test

test命令是一个用于检查文件属性和进行条件测试的工具

文件测试

  • -e filename:判断文件是否存在。
  • -f filename:判断是否为常规文件。
  • -d directory:判断是否为目录。
  • -r filename:判断文件是否可读。
  • -w filename:判断文件是否可写。
  • -x filename:判断文件是否可执行。

示例:

if [ -e myfile.txt ]; then
    echo "文件存在"
fi

字符串比较

  • -z string:判断字符串是否为空。
  • -n string:判断字符串是否非空。
  • string1 = string2:判断两个字符串是否相等。
  • string1 != string2:判断两个字符串是否不相等。

示例:

if [ -z "$myvar" ]; then
    echo "变量为空"
fi

数值比较

  • num1 -eq num2:判断两个数字是否相等。
  • num1 -ne num2:判断两个数字是否不相等。
  • num1 -lt num2:判断num1是否小于num2。
  • num1 -le num2:判断num1是否小于或等于num2。
  • num1 -gt num2:判断num1是否大于num2。
  • num1 -ge num2:判断num1是否大于或等于num2。

示例:

if [ "$a" -gt "$b" ]; then
    echo "$a 大于 $b"
fi

注意事项

  • 使用方括号时,[]之间需要有空格。
  • 使用test命令时,特别是在复杂条件下,可以使用&&(与)和||(或)组合条件。

示例:

if [ -f myfile.txt ] && [ -r myfile.txt ]; then
    echo "文件存在且可读"
fi
posted @   pro111  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示