Featured image of post Shell编程学习 01

Shell编程学习 01

01.shell

Shell 脚本的建立和执行

  1. 脚本第一行指定脚本的解释器
1
#!/bin/bash
  1. source script-name 或者".“读入或加载知道那个的 shell 脚本文件

  2. shell 脚本的变量继承关系 使用 shell 脚本来加载脚本中的变量,使用 source ./file 或者. ./file来加载。直接使用 sh 来执行是无法加载的。原因是 sh file 启动了新的 shell 来执行脚本,当执行结束,新的 shell 退出导致变量无法被继承下来。使用 source 或者.来运行程序,是在当前 shell 中运行,所以脚本中的变量是可以被继承下来的。

shell 编程归还

  1. shell 脚本的第一行指定脚本解释器
1
#!/bin/bash

或者

1
#!/bin/sh
  1. shell 脚本开头加上版本、版权等信息:
1
2
3
4
5
# Date: 17:25 CST 2023-2-3
# Author: Create by Steve Dong
# Blog: https://echowings.github.io
# Description: This scriptes funciton is ...
# Version 1.0
  1. 环境变量初始化与对应文件的生效顺序
  • 通过系统用户登录后默认运行的 shell
graph TD A[ /etc/profile ] --> B( /etc/profile.d ) B --> C[ $HOME/.bash_profile ] C --> D[ $HOME/.bashrc ] D --> E[ /etc/bashrc ]
  • 非登录交互运行的 shell
  • 执行脚本运行非交互式 shell
    graph TD A[ $HOME/.bashrc ] --> B[ /etc/bashrc ]
CC BY-NC-ND
最后更新于 Sep 19, 2024 09:13 UTC
Built with Hugo
主题 StackJimmy 设计