Linux笔记(2)source和bash执行脚本的区别,文件的信息读取

这篇博客详细介绍了Linux中变量定义、脚本执行的差异,包括source和bash命令的区别,以及进程间的父子关系。内容涵盖bashrc脚本执行、环境变量作用域、文件权限设置、软链接创建及文件信息读取等基本操作。通过实例演示了如何在不同bash进程中运行脚本,并展示了如何使变量在子进程中生效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键字:
各种编辑语言里预留的已经给本语言使用的名字

定义变量:
不予许变量名以数字开头

   [root@localhost ~]# bash /root/.bashrc 
    产生一个子进程bash去执行脚本  --》在子进程里运行
    [root@localhost ~]# source /root/.bashrc 
    在当前bash进程里执行脚本  --》在父进程里运行

进程:
父进程
子进程

   [root@localhost ~]# sg="m"
   [root@localhost ~]# mn="s"
   [root@localhost ~]# echo  $sg  $mn 
      m  s
   [root@localhost ~]#
   [root@localhost ~]# vim  output.sh#!/bin/bash          ---》申明这个脚本默认使用/bin/bash这个解释器去执行
   echo $sg
   echo $mn
   city='changsha'echo "
   $city is a good place"
   [root@localhost ~]# source output.sh  在当前bash里执行output.sh
   m  
   s
   changsha is a good place
   [root@localhost ~]# bash output.sh  启动一个子bash进程去执行output.sh
   changsha is a good place
   [root@localhost ~]# export sg mn  输出sg和mn变量为全局变量[root@localhost ~]# bash output.sh 
    m
    s
    changsha is a good place
    [root@localhost ~]#
    [root@localhost ~]# ./output.sh 
    其实也是执行当前目录下的output.sh
   -bash: ./output.sh: Permission denied  权限不拒绝
   [root@localhost ~]# chmod +x output.sh  授予可执行权限
  [root@localhost ~]# ./output.sh  执行当前目录下的output.sh脚本m  s
 changsha is a good place
 [root@localhost ~]# cat output.sh 
 #!/bin/bash
 echo $sg
 echo $mncity='changsha'
 echo "$city is a good place"
 [root@localhost ~]#[root@localhost ~]# chmod -x output.sh 去除可执行权限
-rwxr-xr-x  1 root root   
  77 Nov 20 15:02 output.sh     
           user group

文件的信息读取

           - 代表普通的文件
           - d 代表是文件夹  directory
           - l 代表的是链接文件  link
           - rwxr-xr-x  代表权限  r read 读   w write 写  x execute 执行user group others
           - user--》u
           - group -->g
           - others -->o
           - all  -->a
           [root@localhost ~]# chmod u+x output.sh
            [root@localhost ~]# ll output.sh
             -rwxr--r-- 1 root root 77 Nov 20 15:02 
             output.sh
             [root@localhost ~]# chmod a+x output.sh
              [root@localhost ~]# chmod g+x output.sh
               [root@localhost ~]# chmod o+x output.sh
                [root@localhost ~]# 
                执行: 可以理解为运行这个文件里面的命令
                [root@localhost lianxi]# ls
                hunan
                [root@localhost lianxi]# mkdir  guangdong
                [root@localhost lianxi]# ls
                guangdong  hunan
                [root@localhost lianxi]# ln  -s guangdong 
                yue  创建链接文件yue指向guangdong
                ln 是创建链接文件的命令
                -s  创建符号链接(软链接)--》相当于windows里的快捷方式
                [root@localhost lianxi]# 
                lltotal 0
                drwxr-xr-x 2 root root  6 Nov 20 15:28
                 guangdong
                 drwxr-xr-x 3 root root 22 Nov 13 15:03
                  hunan
                  lrwxrwxrwx 1 root root 10 Nov 20 15:28 yue -> guangdong/
                  [root@localhost lianxi]# ls
                  guangdong  hunan  yue
                  [root@localhost lianxi]# cd yue
                  [root@localhost yue]# mkdir guangzhou
                  [root@localhost yue]# ls
                  guangzhou
                  [root@localhost yue]# cd  ..
                  [root@localhost lianxi]# ls
                  guangdong  hunan  yue
                  [root@localhost lianxi]# cd 
                  guangdong/
                  [root@localhost guangdong]# ls
                  guangzhou
                  [root@localhost guangdong]# 
                  [root@localhost lianxi]# ln -s  hunan   xiang
                               源文件   链接文件
                       [root@localhost lianxi]# ll
                       total 0
                       drwxr-xr-x 3 root root 23 Nov 20 15:28 guangdong
                       drwxr-xr-x 3 root root 22 Nov 13 15:03 hunan
                       lrwxrwxrwx 1 root root  5 Nov 20 15:34 xiang -> hunan
                       lrwxrwxrwx 1 root root 10 Nov 20 15:28 yue -> guangdong/
                       [root@localhost lianxi]#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值