10.docker exec -it /bin/bash报错解决、sh与bash区别

报错

进入容器时,报如下错误

bash 复制代码
dockeruser@dell-PowerEdge-R740:~$ docker exec -it daf2 /bin/bash
OCI runtime exec failed: exec failed: unable to start container process: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown

解决

将bin/bash换成bin/sh

bash 复制代码
dockeruser@dell-PowerEdge-R740:~$ docker exec -it daf2 /bin/sh

分析

制作镜像时使用了精简版,只装了sh命令,未安装bash。

拓展

(1)shell简介

Shell是一种应用程序,该应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。Shell 是一个用 C 语言编写的程序,是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。

sh (Bourne Shell)是一个早期的重要shell,1978年由史蒂夫·伯恩编写,并同Version 7 Unix一起发布。
bash(Bourne-Again Shell)是一个为GNU计划编写的Unix shell。1987年由布莱恩·福克斯创造。主要目标是与POSIX标准保持一致,同时兼顾对sh的兼容,是各种Linux发行版标准配置的Shell,在Linux系统上/bin/sh往往是指向/bin/bash的符号链接。

(2)#!

  • #! 是一个特殊标记,说明这是一个可执行的脚本。除了第一行,其他以#开头的都不再生效,为注释。
  • #! 后面是脚本的解释器程序路径。这个程序可以是shell,程序语言或者其他通用程序,常用的是bash、sh。

(3)sh与bash区别

  • sh 跟bash的区别是bash是否开启POSIX模式。

  • sh是bash的一种特殊的模式,sh就是开启了POSIX标准的bash, /bin/sh 相当于 /bin/bash --posix。

  • 在Linux系统上/bin/sh往往是指向/bin/bash的符号链接

    ln -s /bin/bash /bin/sh

  • sh 遵循POSIX规范:"当某行代码出错时,不继续往下解释"。bash 就算出错,也会继续向下执行。

hello.sh脚本:

bash 复制代码
#!/bin/sh
source err
echo "sh hello"

hello2.sh脚本:

bash 复制代码
#!/bin/bash
source err
echo "bash hello2"

输出结果:

bash 复制代码
[root@admin]# bash hello2.sh 
hello2.sh: line 2: err: No such file or directory
hello2
[root@admin]# sh hello.sh 
hello.sh: line 2: source: err: file not found
[root@admin]# bash --posix hello2.sh 
hello2.sh: line 2: source: err: file not found
相关推荐
玄同7652 分钟前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
2401_8589368812 分钟前
【Linux C 编程】标准 IO 详解与实战:从基础接口到文件操作实战
linux·c语言
Yorlen_Zhang13 分钟前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
lxl130714 分钟前
C++算法(1)双指针
开发语言·c++
Roc.Chang24 分钟前
Ubuntu 下 VLC 无法启动(Segmentation fault)终极解决方案
linux·ubuntu·vlc·媒体播放
不绝19125 分钟前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
无小道31 分钟前
Qt-qrc机制简单介绍
开发语言·qt
zhooyu38 分钟前
C++和OpenGL手搓3D游戏编程(20160207进展和效果)
开发语言·c++·游戏·3d·opengl
HAPPY酷41 分钟前
C++ 和 Python 的“容器”对决:从万金油到核武器
开发语言·c++·python
大鹏说大话41 分钟前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#