0 导学
概述
本文的Shell环境为
![](https://i-blog.csdnimg.cn/blog_migrate/dcf4c57345c593145c0541cdfb79714f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ab2d148609d6ff5bec20f0f99da4c85d.png)
编程语言分类 | 特点 | |
---|---|---|
编译型 | C/C++ Java | 效率高 |
解释型 | Python/JavaScript/ Perl /ruby/Shell | 跨平台性好(兼容性好) |
Shell种类
k@LAPTOP-L3K0ADUG:/mnt/c/Users/K$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh # ->bash 快捷方式 (Ubuntu默认采用dash,后面会改)
/bin/bash # 大多数Linux默认的shell,包含的功能几乎可涵盖shell所有功能
/bin/rbash
/bin/dash #小巧,高效,功能相对少
/usr/bin/tmux
/usr/bin/screen
# 以下myUbuntu未显示
/sbin/nologin # 表示非交互,不能登录操作系统
/bin/tcsh # csh增强版,完全兼容csh
/bin/csh # 具有C语言风格的shell,有许多特性,也有缺陷
k@LAPTOP-L3K0ADUG:/mnt/c/Users/K$ echo $SHELL
/bin/bash # myUbuntu默认解析器
切换sh为bash
Ubuntu切换默认sh为bash或dash可用
sudo dpkg-reconfigure dash
命令
k@LAPTOP-L3K0ADUG:/bin$ ll | grep sh
-rwxr-xr-x 1 root root 1113504 Jun 7 2019 bash*
-rwxr-xr-x 1 root root 121432 Jan 25 2018 dash*
lrwxrwxrwx 1 root root 4 Jun 7 2019 rbash -> bash*
lrwxrwxrwx 1 root root 4 Aug 22 2020 sh -> dash*
lrwxrwxrwx 1 root root 4 Jan 25 2018 sh.distrib -> dash*
lrwxrwxrwx 1 root root 7 Mar 7 2019 static-sh -> busybox*
k@LAPTOP-L3K0ADUG:/bin$ sudo dpkg-reconfigure dash
[sudo] password for k:
# 此处有多行空白 会进入UI界面 选择否(如下图)
Removing 'diversion of /bin/sh to /bin/sh.distrib by dash'
Adding 'diversion of /bin/sh to /bin/sh.distrib by bash'
Removing 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by dash'
Adding 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by bash'
k@LAPTOP-L3K0ADUG:/bin$ ll | grep sh
-rwxr-xr-x 1 root root 1113504 Jun 7 2019 bash*
-rwxr-xr-x 1 root root 121432 Jan 25 2018 dash*
lrwxrwxrwx 1 root root 4 Jun 7 2019 rbash -> bash*
lrwxrwxrwx 1 root root 4 Mar 11 10:05 sh -> bash*
lrwxrwxrwx 1 root root 4 Aug 22 2020 sh.distrib -> dash*
lrwxrwxrwx 1 root root 7 Mar 7 2019 static-sh -> busybox*
![](https://i-blog.csdnimg.cn/blog_migrate/a6b3a6f9c58ccd396bbe859c3ca9872e.png)
脚本测试
k@LAPTOP-L3K0ADUG:/mnt/d/BigData/Shell$ vim test.sh
k@LAPTOP-L3K0ADUG:/mnt/d/BigData/Shell$ cat test.sh
#!/bin/bash
# Author: K
# Desc: My first shell.
# Path: /mnt/d/BigData/Shell
# Usage: 1.sh test.sh 或 bash test.sh 2.chmod +x test.sh 或 chmod 777 test.sh 和 test.sh
# Update: 2022/03/10
rm -rf /tmp/*
mkdir /tmp/dir1
mkdir /tmp/dir2
mkdir /tmp/dir3
cp /etc/hosts /tmp/dir1
echo "The test has been finished at $(date +%F)"
k@LAPTOP-L3K0ADUG:/mnt/d/BigData/Shell$ bash test.sh
The test has been finished at 2022-03-10
k@LAPTOP-L3K0ADUG:/mnt/d/BigData/Shell$ ls /tmp/
dir1 dir2 dir3
k@LAPTOP-L3K0ADUG:/mnt/d/BigData/Shell$ ls /tmp/dir1
hosts
# 标准方式执行脚本(+可执行权限)
k@LAPTOP-L3K0ADUG:/mnt/d/BigData/Shell$ chmod +x test.sh
k@LAPTOP-L3K0ADUG:/mnt/d/BigData/Shell$ test.sh
test.sh: command not found
k@LAPTOP-L3K0ADUG:/mnt/d/BigData/Shell$ ./test.sh
The test has been finished at 2022-03-11
cp /etc/hosts /tmp/dir1
若dir1
目录存在则复制到dir1目录下,若不存在就复制到tmp
目录下重命名为dir1
脚本执行方式
- 采用sh/bash+脚本的相对/绝对路径(不用赋予脚本执行权限)
相对路径可以写成test.sh
- 采用脚本的相对/绝对路径执行脚本(必须具有可执行权限)
相对路径一定要写成./test.sh
,而不是test.sh
,运行其它二进制的程序也一样. 直接写 test.sh,linux 系统会去PATH
里寻找有没有叫 test.sh 的,而只有/bin
/sbin
/usr/bin
/usr/sbin
等在PATH里,当前目录通常不在PATH里,所以写成test.sh是找不到命令的,要用 ./test.sh 告诉系统就在当前目录找
为方便自动生成开头注释 拷贝到用户家目录(~)下并命名为.vimrc
To be updated…