Shell

0 导学

概述

本文的Shell环境为

编程语言分类特点
编译型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*

脚本测试

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/dir1dir1目录存在则复制到dir1目录下,若不存在就复制到tmp目录下重命名为dir1
脚本执行方式

  1. 采用sh/bash+脚本的相对/绝对路径(不用赋予脚本执行权限)
    相对路径可以写成test.sh
  2. 采用脚本的相对/绝对路径执行脚本(必须具有可执行权限)
    相对路径一定要写成./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…

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kukukukiki192

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值