Linux系统入门-Bash

Linux系统入门-Bash,第1张

Shell 是一种命令行解释器, 其读取用户输入的字符串命令, 解释并且执行命令;它是一种特殊的应用程序, 介于系统调用/库与应用程序之间, 其提供了运行其他程序的的接口;它可以是交互式的, 即读取用户输入的字符串也可以是非交互式的, 即读取脚本文件并解释执行, 直至文件结束. 无论是在类 UNIX, Linux 系统, 还是 Windows, 有很多不同种类的 Shell: 如类 UNIX, Linux 系统上的 Bash, Zsh 等Windows 系统上的 cmd, PowerShell 等.

Bash 是 Bourne Again SHell 的缩写, 是 GNU 计划中的 Shell, 也是一些类 UNIX 系统与多数 Linux 发行版的默认 Shell

使用Shell可以实现对Linux系统实现绝大部分的管理,例如:

#获取当前时间

[root@CentOS7 ~]# date

Mon Mar 15 22:59:47 CST 2021

#创建文件

[root@CentOS7 opt]# touch xcz

[root@CentOS7 opt]# ll

-rw-r--r--. 1 root root 0 Mar 15 23:01 xcz

#创建一百个文件,我们一般就会使用shell script进行创建

[root@CentOS7 opt]# cat touch.sh

#!/bin/bash

for n in `seq 100`do

touch xcz$n &&

echo "文件xcz$n创建成功哦!"

done

[root@CentOS7 opt]# sh touch.sh

命令行输入方式:效率较低,适用于工作量不大的工作;

shell script 脚本方式:效率高,适用于工作量大且复杂的工作。

[root@CentOS7 opt]# bash --version

GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

Copyright (C) 2011 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later

This is free softwareyou are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

'#39= 普通用户

'#' = root用户(超级管理员)

#查看当前用户

[root@CentOS7 ~]# whoami

root

#查看当前命令提示符

[root@CentOS7 ~]# echo $PS1

[u@h W]$

root:当前系统的用户

CentOS7:当前系统的主机名

~:当前所在的位置

#:超级管理员身份(root用户)

$:普通用户

提示符参数及含义

d :代表日期

H :完整的主机名称

h :仅取主机名中的第一个名字

:显示时间为24小时格式,如:HH:MM:SS

T :显示时间为12小时格式

A :显示时间为24小时格式:HH:MM

u :当前用户的账号名称

v :BASH的版本信息

w :完整的工作目录名称

W :利用basename取得工作目录名称,只显示最后一个目录名

# :下达的第几个命令

$ :提示字符,如果是root用户,提示符为 "#" ,普通用户则为 "#34

#颜色

30 40 黑色

31 41 红色

32 42 绿色

33 43 黄色

34 44 蓝色

35 45 紫红色

36 46 青蓝色

37 47 白色

PS1='[e[3240m] [[u@h w ]$ [e[0m]'

PS1="[e[3740m][[e[3240m]u[e[3740m]@h [e[3640m]w[e[0m]]$ "

PS1="[e[3740m][[e[3240m]u[e[3740m]@[e[3540m]h[e[0m] [e[3640m]w[e[0m]]$ "

#提示符的应用

[root@CentOS7 ~]# vi .bashrc

#最后一行下面添加

PS1="[e[3740m][[e[3240m]u[e[3740m]@[e[3540m]h[e[0m] [e[3640m]w[e[0m]]$ "

#使用source生效

[root@CentOS7 ~]# source .bashrc

#命令 选项 参数

command [-options] [arguments]

[root@CentOS7 ~]# ls -l /opt/

#命令:整条shell命令的主体

#选项:用于调节命令的具体功能

#以'-'引导段个事选项(单个字符),例如”-l“

#以'--'引导长格式选项(多个字符),例如”--list“

#多个短格式选项可以卸载一起,只用一个”-“引导,例如”-la“

#参数:命令 *** 作与偶的对象,如文件、目录名等

#命令必须开头,选项和参数位置可以发生变化

我们在使用Linux系统进行查找一个多层级的文件时,我们可以使用键盘上的Tab键进行快速补全

补全的形式有:

#如果我们忘记网络配置文件具体路径,那么我们就可以使用补全的形式进行配置

[root@CentOS7 ~]# vi /etc/sysconfig/

anaconda cpupower grub irqbalance modules/ rdisc selinux

authconfig crond init kdump netconsole readonly-root sshd

cbq/ ebtables-config ip6tables-config kernel network rsyslog wpa_supplicant

console/ firewalld iptables-config man-db network-scripts/ run-parts

[root@CentOS7 ~]# vi /etc/sysconfig/network

network network-scripts/

[root@CentOS7 ~]# vi /etc/sysconfig/network-scripts/if

ifcfg-ens33 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team

ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort

ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel

ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless

[root@CentOS7 ~]# vi /etc/sysconfig/network-scripts/ifcfg-

ifcfg-ens33 ifcfg-lo

[root@CentOS7 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33

#如果你的Linux系统无法进行补全,那么咱们可以安装一个扩展包即可

[root@CentOS7 ~]# yum install -y bash-completion

clear #或者用快捷键 ctrl + l

ctrl+c #有些程序也可以用q键退出

ctrl+z # 进程会挂起到后台

bg jobid # 让进程在后台继续执行

fg jobid # 让进程回到前台

Ctrl键+a #将当前光标移动到命令行的行首

Ctrl键+e #将当前光标移动到命令行的行尾

Ctrl键+u #将当前光标之前的所有字符剪切

Ctrl键+k #将当前光标之后的所有字符剪切

Ctrl键+w #将当前光标之前的字符剪切,以空格为结尾

Ctrl键+d #退出当前会话窗口

Ctrl键+z #将当前前台运行的程序,放到后台运行

Ctrl键+r #搜索 历史 命令

Ctrl键+y #粘贴剪切板上的内容

Ctrl键+左右方向键 #向指定的方向键移动一组字符,以空格为分隔符

ESC键+. #使用上一条命令的最后的参数或者路径,以空格为分隔符,空格之后的内容,delete键 从前往后删除一个字符

!命令 #执行最近的一次以该命令为开头的命令

!! #执行上一条命令

#使用格式:

[命令] + [--help] 或者[man] + [命令] 即可

#例如touch命令帮助

[root@CentOS7 ~]# touch --help

Usage: touch [OPTION]... FILE...

Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h

is supplied.

A FILE argument string of - is handled specially and causes touch to

change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.

-a change only the access time

-c, --no-create do not create any files

-d, --date=STRING parse STRING and use it instead of current time

-f (ignored)

-h, --no-dereference affect each symbolic link instead of any referenced

file (useful only on systems that can change the

timestamps of a symlink)

-m change only the modification time

-r, --reference=FILE use this file's times instead of current time

-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time

--time=WORD change the specified time:

WORD is access, atime, or use: equivalent to -a

WORD is modify or mtime: equivalent to -m

--help display this help and exit

--version output version information and exit

Note that the -d and -t options accept different time-date formats.

GNU coreutils online help:

For complete documentation, run: info coreutils 'touch invocation'

     linux中bash是什么?我们一起来了解一下吧。

      bash是指GNU项目编写的中的Unixshell,也就是指的linux所用的shell,而Shell是指提供使用者使用界面的软件,也就是一个命令行解释器,BASH是SHELL中的一种,是大多数LINUX发行版默认的SHELL。

      linux系统与windows系统的区别

      区别1:开放性

      所谓的开放性就是linux *** 作系统是开放源码系统,可以对其程序进行编辑修改。而微软的windows系统是手微软版权保护,就是只能微软内部进行开发及修改。

      区别2:价格不同

      linux系统是免费使用,而微软开发的windows系统则是需要花费金钱去购买。

      区别3:文件格式不同

      windows *** 作系统内核是NT,而linux是shell另外,windows硬盘文件格式是fat32或NTSF,而linux需要的文件格式是ext2或ext3,该 *** 作系统还多一个SWAP格式的交换分区。

      本文章基于ThinkpadE15品牌、centos7系统撰写的。

Bash,Unix

shell的一种,在1987年由布莱恩·福克斯为了GNU计划而编写。1989年发布第一个正式版本,原先是计划用在GNU *** 作系统上,但能运行于大多数类Unix系统的 *** 作系统之上,包括Linux与Mac

OS X v10.4都将它作为默认shell。

Bash是一个命令处理器,通常运行于文本窗口中,并能执行用户直接输入的命令。Bash还能从文件中读取命令,这样的文件称为脚本。和其他Unix

shell

一样,它支持文件名替换(通配符匹配)、管道、here文档、命令替换、变量,以及条件判断和循环遍历的结构控制语句。包括关键字、语法在内的基本特性全部是从sh借鉴过来的。其他特性,例如历史命令,是从csh和ksh借鉴而来。总的来说,Bash虽然是一个满足POSIX规范的shell,但有很多扩展。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7374288.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存