SHELL入门

演示环境为rhel7.5

[root@cdhmanager ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 7.5 (Maipo)
[root@cdhmanager ~]# uname -a
Linux cdhmanager 3.10.0-862.el7.x86_64 #1 SMP Wed Mar 21 18:14:51 EDT 2018 x86_64 x86_64 x86_64 GNU/Linux

常用操作系统默认shell
linux默认的shell是Bourne Again shell (bash),Solaris和FreeBSD下默认是Bourne shell(sh),AIX下默认是K偶让你shel(ksh)

查看默认shell

[root@cdhmanager ~]# echo $SHELL
/bin/bash
[root@cdhmanager ~]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Jul 21 10:38 /bin/sh -> bash   sh软连接到bash所以用sh和bash效果一样
[root@cdhmanager ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

查看shell版本

[root@cdhmanager ~]# bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

查看系统中有哪些shell

[root@cdhmanager ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

配置vi=vim

[root@cdhmanager ~]# echo "alias vi='vim'" >>/etc/profile      追加字段到文件
[root@cdhmanager ~]# tail -l /etc/profile  查看文件末尾,可以看到追加的字段
alias vi='vim'
[root@cdhmanager ~]# source /etc/profile  执行文件,此处可以用source或.(点)不能用sh,因为sh是启一个子shell,如果脚本需要在当前shell起作用就需要用source或.

脚本开头第一行
如果不是在第一行写就表示注释了,这一行代表着脚本解释器用bash,linux默认都是bash所以在linux系统执行不写也行,如果在别的unix平台执行就有问题,所以建议习惯性写上。

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

常用脚本开头写法

#!/bin/sh
#!/bin/bash
#!/usr/bin/awk
#!/bin/sed
#!/usr/bin/tcl
#!/usr/bin/expect
#!/usr/bin/perl
#!/usr/bin/env python

执行脚本的方法
1、sh test.sh 推荐的方法
2、cat test.sh |bash
3、sh <test.sh
4、root/test.sh 执行权限不够,需要增加执行权限
chmod +x /root/test.sh
5、. root/test.sh 用点执行会在父shell中执行,不开启子shell,定时任务中会用到
6、 source root/test.sh 等价于点执行会在父shell中执行,不开启子shell,

二、环境变量
环境变量名称均采用大写
系统自带变量

[root@cdhmanager ~]# echo $USER
root
[root@cdhmanager ~]# echo $HOME
/root

自定义环境变量

[root@cdhmanager ~]# export fan=1
[root@cdhmanager ~]# echo $FAN
1

环境变量永久生效文件优先顺序
/etc/bashrc
~/.bashrc
~/.bash_profile
/etc/profile ssh登录后会自动加载该文件,如果执行定时任务,不能用这个,要用第一个bashrc

取消环境变量
unset FAN(变量名,不加$)

普通变量(局部变量)定义赋值经验
单引号’’ awk比较特殊,单引号是解析,双引号是不解析
单引号里面有什么就输出什么

[root@cdhmanager ~]# b='1234 $USER'
[root@cdhmanager ~]# echo $b
1234 $USER

双引号" "
双引号中如果有命令,变量等输出时会显示相应的命令、变量结果

[root@cdhmanager ~]# b="1234 $USER"
[root@cdhmanager ~]# echo $b
1234 root

[root@cdhmanager ~]# D="1234`ls` $USER"
[root@cdhmanager ~]# echo $D
1234anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos root

无引号
一般连续的数字可以不加引号,如果有空格等可能无法完整输出,建议使用双引号代替

反引号
一般用于引用命令等价于$()

大括号{},输出的时候有组合的时候里面包含有变量名称的时候要用大括号

[root@cdhmanager ~]# a=1
You have mail in /var/spool/mail/root
[root@cdhmanager ~]# echo $abcd

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

输出变量,echo printf 单引号,双引号跟定义里面的用法一样,

[root@cdhmanager ~]# echo '$a'
$a
[root@cdhmanager ~]# echo "$a"
1

待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值