Lecture 2 LINUX Shells¶
2.1 Putty¶
- translation
SSH登录SSH - Auth\(\to\) 文件登录:公钥文件(远程计算机),私钥文件(登录密码,认可用户身份)
2.2 Command Interpreter-shell¶
- Various Unix Shells: bash (Linux的默认shell),
csh,sh,ksh -
Choose shell
- Set in the file:
/etc/passwd - Change default shell:
chsh - Change current shell
- Set in the file:
-
shell 的三个自启动文件 (Bash startup file)
/etc/profile: system environment- 所有的
system配置文件都保存在etc目录下以文本格式存放 system启动时执行
- 所有的
$HOME/. bash_profile: execute once when logging on- 带
.表示的是隐藏文件 - 每个用户登录时
nano .bash_profile
- 带
$HOME/.bashrc: execute each time fork a shell- 在
bash启动时执行 - 只针对
bash nano .bashrc
- 在
作用:对系统进行配置,如环境变量
2.3 Environment variables¶
环境变量 :在内存中,保持在操作系统中的变量,当程序需要某些信息时,从约定好的环境变量中读取值
CLASSPATH
CLASSPATH = %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin 指定到哪里去找 java 调用的库函数(缺省的库函数)
PS: %JAVA_HOME% (宏变量):java 的安装目录
否则 需要通过 java -cp ... 目录 查找环境变量
PATH
如,输入 calc 先在当前目录下找可执行文件,然后再在 PATH 环境变量下找可行性文件
2.3.1 Check Environment Path¶
在 Linux 中使用
echo $PATH(name)env
注意:在 Linux 中不会优先在当前目录中找可执行程序,而是在环境变量中
如果需要在
- 添加路径信息
- 把当前目录加到环境变量的后面
其中 . 就是添加的目录
- 把当前目录加到环境变量的前面
其中 . 就是添加的目录
2.3.2 Commands¶
-
View environment variables
echo:view a single environment variablesenv, set: view all environment variables
-
Common environment variables
–$PATH:command searching path–$HOME:user logging on directory–$PS1:command prompt\hfirst part of machine\sshell name(i.e.“bash”)\uuser name\wcurrent work directory(i.e.“/home/s1”)\$if not super user (root):"$";if supper user:"#"
–$LANG: language (shell和用户交互的编码方式)
2.4 Linux Command¶
2.4.1 Command struct¶
–command [[-]option(s)][option argument(s)] [command argument(s)]
格式:命令 -选项 -选项参数 -命令参数
2.4.2 Command key¶
- Command auto complement :
TAB - 快速执行某个命令
!+ 命令的前缀 - 执行刚刚执行的命令:
!! -
Hot key
Ctrl + u: delete characters to the beginCtrl + k: delete characters to the endCtrl + c: cancel the commandCtrl + a: move the cursor to the beginCtrl + e: move the cursor to the end
-
alias: create pseudonyms (nicknames) for commands
- Create alias
alias [[name]=string]
- Disable alias
unalias name
- Create alias
- type
2.4.3 Command execute order¶
-
alias
-
Build-in command
-
Execute file
2.4.4 Shell Metacharacters¶
shell 元字符
| Metacharacters | purpose | Example |
|---|---|---|
| $ | Dereference shell variable | echo $PATH |
| “ | To quote multiple characters but allow substitution 对其中的元字符做特殊解释 | echo “$PWD” |
| ‘ | To quote multiple characters 不对其中的元字符做特殊解释 | echo ‘$PWD’ |
| * | To match zero or more characters 任意多个任意字符 | rm *.tmp |
| ? | To substitute a wild card for exactly one characters 对应一个字符 | rm ?.tmp |
| [ ] | To insert wild cards 填区间值 | rm [1-3].tmp |
| | | To substitute a command 命令替换 | PATH=$PATH:pwd |
| ~ | To name home directory 快速访问到工作目录 | ls ~/ |
| \ | To escape a single character 转义符,转为普通字符,删除空格使用 | rm a\ b\ c |
pwd表示当前所在目录
2.4.5 view system information¶
whoami(id):user identityhostname:host namehostname name修改机器名
uname:OS informationuname -a操作系统的所有信息
free:Memoryfree -h
date:system datedate +%y%m%ddate +%Y%m%d
- view the files in the
/proc
Last update:
August 27, 2023
Created: August 27, 2023
Created: August 27, 2023