shell基础 第一部分 (特殊符号,变量)
来源:互联网 发布:web编程语言 编辑:程序博客网 时间:2024/06/05 08:02
第一部分:
1.history 用来查看命令历史
[root@niejicai-linux ~]# history 查看命令历史
[root@niejicai-linux ~]# ls
10.txt 34.txt.zip 999.txt
12 3.txt abc.zip
[root@niejicai-linux ~]# !! 表示上一次执行的命令
ls
10.txt 34.txt.zip 999.txt
12 3.txt abc.zip
[root@niejicai-linux ~]# ls /etc/passwd
/etc/passwd
[root@niejicai-linux ~]# ls !$ 上一条命令的最后一个参数
ls /etc/passwd
/etc/passwd
[root@niejicai-linux ~]#history
999 ls
1000 history
1001 ls
1002 ls /etc/passwd
1003 history
-------
[root@niejicai-linux ~]# !999 表示执行命令历史的第999条命令
ls
10.txt 34.txt.zip 999.txt
12 3.txt abc.zip
234 4.txt anaconda-ks.cfg
2.txt 5.txt etc
33.tar 6.txt install.log
33.tar.bz2 7.txt install.log.syslog
33.tar.gz 90.txt jicai
33.tar.xz 91.txt niejicailinux.txt
34.tar 92.txt root
34.txt 93.txt
[root@niejicai-linux ~]#history
--------------
995 mv /etc/yum.repos.d.bak/ /etc/yum.repos.d
996 ls
997 cd
998 cd /etc/yum.repos.d/
999 ls
1000 history
1001 ls
1002 ls /etc/passwd
1003 history
1004 ls
1005 history
[root@niejicai-linux ~]# !cd 表示执行history 记录中的从下到上的第一个个带有cd的命令
cd /etc/yum.repos.d/
[root@niejicai-linux yum.repos.d]#
[root@niejicai-linux ~]# !n !是执行上一次命令n开头的命令
最后一次
2.
[root@niejicai-linux ~]# ls /tmp/*.txt *表示通配
/tmp/116.txt /tmp/21.txt
/tmp/1.txt /tmp/user4.txt
[root@niejicai-linux ~]# ls [12].txt [] 里面任选一个
2.txt
[root@niejicai-linux ~]# ls /etc/passw? ?表示一个任意的字符
/etc/passwd
[root@niejicai-linux ~]#
3.
> 从定向, 从定向的之后,以前的内容会被清空,从新写入新的内容
>> 追加, 在最后一个行加上要写人的内容,用来的内容不会被删除
<反向从定向 , 例如cat <2.txt就相当于cat.2.txt
[root@niejicai-linux ~]# cat < 2.txt
1111111
11111111111
[root@niejicai-linux ~]# cat 2.txt
1111111
11111111111
[root@niejicai-linux ~]#
反向从定向的用法
[root@niejicai-linux ~]# mail -s "987876" 8888@qq.com <2.txt
4.
2> 错误重定向
1> 正确重定向,但是1可以不写
[root@niejicai-linux ~]# ls niejicai 2> error.log 错误从定向
[root@niejicai-linux ~]# ls error.log
error.log
[root@niejicai-linux ~]# cat error.log
ls: cannot access niejicai: No such file or directory
[root@niejicai-linux ~]# ls niejicai 2>> error.log 错误的追加
[root@niejicai-linux ~]# cat error.log
ls: cannot access niejicai: No such file or directory
ls: cannot access niejicai: No such file or directory
[root@niejicai-linux ~]#
应用:
[root@niejicai-linux ~]# sh aaa.sh > 1.log 2> 1.log 在执行 aaa.sh的时候,把正确的和错误的到输入到 1.log
等于如下:[root@niejicai-linux ~]# sh aaa.sh > 1.1og 2>&1
也支持追加: [root@niejicai-linux ~]#sh aaa.sh >>1.log 2>>&1
5.| 管道
[root@niejicai-linux ~]# ls
10.txt 34.txt.zip 999.txt
12 3.txt abc.zip
234 4.txt anaconda-ks.cfg
2.txt 5.txt error.log
33.tar 6.txt etc
33.tar.bz2 7.txt install.log
33.tar.gz 90.txt install.log.syslog
33.tar.xz 91.txt jicai
34.tar 92.txt niejicailinux.txt
34.txt 93.txt root
[root@niejicai-linux ~]# ls | xargs
10.txt 12 234 2.txt 33.tar 33.tar.bz2 33.tar.gz 33.tar.xz 34.tar 34.txt 34.txt.zip 3.txt 4.txt 5.txt 6.txt 7.txt 90.txt 91.txt 92.txt 93.txt 999.txt abc.zip anaconda-ks.cfg error.log etc install.log install.log.syslog jicai niejicailinux.txt root
[root@niejicai-linux ~]# cat 2.txt | more 管道,前一个命令的输出,把输出结果当作后一个命令的输入。
1111111
11111111111
[root@niejicai-linux ~]#
6.作业控制:
ctrl +z,jobs , fg ,bg
[root@niejicai-linux ~]# sleep 200 休眠200s
^Z ctrl +c 终止
[3]+ Stopped sleep 200 ctrl +z 暂停一个任务
[root@niejicai-linux ~]# jobs jobs查看后台的任务
[2]- Stopped sleep 100
[3]+ Stopped sleep 200
[root@niejicai-linux ~]# fg 2 fg 把一个任务调回来,fg后面
加任务的id。如果后面不加任务id默认去执行有+这个,因为这个优先级比较高。
sleep 100
^Z
[2]+ Stopped sleep 100
[root@niejicai-linux ~]# fg 3
sleep 200
^Z
[3]+ Stopped sleep 200
[root@niejicai-linux ~]# fg
sleep 200
^Z
[3]+ Stopped sleep 200
[root@niejicai-linux ~]#
7.变量
echo
[root@niejicai-linux ~]# echo $ HOME
$ HOME
[root@niejicai-linux ~]# echo $ PATH
$ PATH
[root@niejicai-linux ~]# a=11 赋值了之后就可以输出
[root@niejicai-linux ~]# echo $a
11
[root@niejicai-linux ~]# echo $b
[root@niejicai-linux ~]#
env
env 可以列出当前用户的所有环境变量以及用户自定义全局变量
set
set命令可以把所有变量列出来包括系统的和自定义的全局变量以及当前shell自定义变量
[root@niejicai-linux ~]# export a=1 全局声明
[root@niejicai-linux ~]# bash
[root@niejicai-linux ~]# echo $a
1
[root@niejicai-linux ~]# a=1 当前声明
[root@niejicai-linux ~]# echo $a
1
变量名,不可以与数据开头
比如,这个是不对的
[root@niejicai-linux ~]#3a=1
[root@niejicai-linux ~]#a-1=1
但是下面
[root@niejicai-linux ~]#a='niejicai linux' 单引号,比双引号更加严谨
这个是对的。
变量的内容可以叠加
[root@niejicai-linux ~]# a=1
[root@niejicai-linux ~]# b=$a"123"
[root@niejicai-linux ~]# echo $b
1123
[root@niejicai-linux ~]# c=$a$b
[root@niejicai-linux ~]# echo $c
11123
[root@niejicai-linux ~]#
、
1.2/etc/下面的四个文件
[root@niejicai-linux ~]# ls /etc/profile /etc/bashrc ~/.bashrc ~/.bash_profile
/etc/bashrc /etc/profile /root/.bash_profile /root/.bashrc
[root@niejicai-linux ~]#
是系统的,是全局的,针对任何用户的。
/etc/bashrc
/etc/profile
针对用户自己的。
/root/.bash_profile
/root/.bashrc
[root@niejicai-linux ~]# echo $PS1
[\u@\h \W]\$
[root@niejicai-linux ~]# \u=username \h=hostname \w=path \$=[$#]
[root@niejicai-linux ~]# PS1='[\h@\u \w] \$'
[niejicai-linux@root ~] #ls
10.txt 33.tar.bz2 34.txt.zip 7.txt 999.txt install.log
12 33.tar.gz 3.txt 90.txt abc.zip install.log.syslog
234 33.tar.xz 4.txt 91.txt anaconda-ks.cfg jicai
2.txt 34.tar 5.txt 92.txt error.log niejicailinux.txt
33.tar 34.txt 6.txt 93.txt etc root
[niejicai-linux@root ~] #cd /tmp/
[niejicai-linux@root /tmp] #cd /etc/init.d/
[niejicai-linux@root /etc/init.d] #PS1='[\u@\h \w]\$ '
[root@niejicai-linux /etc/init.d]# cd
在bash中,
.相对于source
测试:
[root@niejicai-linux ~]# vim .bash_profile
[root@niejicai-linux ~]# vim .bashrc
[root@niejicai-linux ~]# cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
echo "profile"
[root@niejicai-linux ~]# cat .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
echo "bashrc"
[root@niejicai-linux ~]#
接着测试
[root@niejicai-linux ~]# bash
bashrc
[root@niejicai-linux ~]# exit
exit
[root@niejicai-linux ~]# su -
bashrc
profile
[root@niejicai-linux ~]# cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
echo "profile"
[root@niejicai-linux ~]#
[root@niejicai-linux ~]# ls .bash_history
.bash_history
[root@niejicai-linux ~]# ls .bash_logout
.bash_logout
[root@niejicai-linux ~]#
8.特殊符号
# 表示注释
|管道
\ 脱译
$ 用在变量的前面
; 多个命令写在一行可以用;号 (cd /tmp/; ls)
~ 家目录
& (两个目录的连接符,ls && cd /tmp/)
( 前台的任务放在后台去 sleep 100 &)
> >> 2> 2>> 从定向
[] 括号中,任意选择一个
0 0
- shell基础 第一部分 (特殊符号,变量)
- shell特殊符号及变量
- 第一部分 Shell基础编程——第二章 变量和运算符
- bash shell (2) 特殊符号变量
- 常用shell特殊符号变量一览
- 第一部分 Shell基础编程——第一章 Shell简介
- JavaScript基础(第一部分)
- Linux -- 第一部分 shell
- 【Shell 编程基础第一部分】第一个Shell脚本HelloShell及一些简单的Shell基础书写与概念;
- 【Shell 编程基础第一部分】第一个Shell脚本HelloShell及一些简单的Shell基础书写与概念
- 【Shell 编程基础第一部分】第一个Shell脚本HelloShell及一些简单的Shell基础书写与概念
- Java基础部分-《第一部分》
- (2)第一个shell脚本和shell变量
- SQL笔记第一部分(select基础)
- http权威指南 第一部分(基础)
- HTML基础整理(第一部分)
- iBATIS第一部分基础
- oracle基础第一部分
- CentOS7下Firewall常用命令
- Volley以及用Volley实现照片墙
- 代码中动态设置view或布局的宽高
- 进入相册选择图片的方法
- 解决搭载OpenStack时候出现can't connect to mysql server on 'controller'的问题
- shell基础 第一部分 (特殊符号,变量)
- shell命令
- 用 canvas 做个好玩的网站背景
- java.lang.Exception: org.apache.tapestry.BindingException
- js实现的一个简单的百分比进度条
- 华为机试——计算数的二进制中第一个1之后0的个数
- MYSQL存储过程执行用字符串拼成的sql语句
- C语言_分支结构和循环结构
- C++primer plus第六版课后编程练习答案6.2