2- command line entry
2019.9.14
Five command-line tool
- Binary executable file
- shell built-in commands
- Interpreted scripting
- shell functions
- Aliases
- shell functions
$ fac() { (echo 1; seq $1) | paste -s -d\* | bc}
$ fac 5
120
- shell function is performed by the function own shell, with us is the function performed by Bash
- We define a FAC () function to generate a string of numbers using seq, paste function with these numbers into line with * and spaced apart, this equation is then passed to BC, which it evaluates and displays the result
- File
~/.bashrc
is Bash configuration file, all of the shell functions can be made at this definition, such benefits are shell functions readily available
- Aliases
$ alias l = 'ls -1 --group-directories-first'
- Alias no parameters, so FAC () function can not be defined with such aliases.
- Aliases can reduce the number of keystrokes;
- Aliases are often defined in the '.bashrc' or '.bash_aliases' profile
Run with no arguments
alias
to see all the parameters of the alias. The default alias on ubuntu have the following general, have to say, I do not look or really do not know$alias alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto'
type -a
usage
$ type -a cd
cd is a shell builtin
$type -a fac
fac is a function
fac ()
{
( echo 1;
seq $1 ) | paste -s -d\* | bc
}
A combination of command-line tool - a combination of pipeline
- The output of tool a tool passed to the next, which can continue to pass virtually without restriction
$ seq 30 | grep 3
3
13
23
30
# 生成1-30的序列传给 grep,筛选出含有3的元素显示
# grep: 筛选元素
$ seq 30 | grep 3 | wc -l
4
# 在上一步的基础上统计行数
# wc: 计数功能;参数 -l 只输出行数量
Input and output redirection
- Pipeline in the last command-line tool is output to the terminal, we can also save it to a file;
# 我们在当下目录下新建文件 test
$ seq 1 > test
$ cat test
1
# 用序列1 覆盖test文件
$ seq 3 >> test
$ cat test
1
1
2
3
# 在test文件末尾加上序列3
$ echo -n "hello" > test
$ cat test
hello$...
# echo -n:就像上面的效果,hello 之后紧接着就是下一个 bash 语句
$ echo "hello" > test
$ cat test
hello
$ ...
# 没有参数 -n,hello后面相当于有个 \n;
# 同样的,> 和 >> 的区别在于覆盖还是在末尾添加
$ echo "hello, world" >> test
$ cat test
hello
hello, world
$ cat test | wc -w
3
# 使用管道组合,wc -w 这个参数表示只统计单词数量
# 这条语句也有很多的等价写法:
$ < test wc -w
3
$ wc -w test
3 test
# 这些等价写法的好处在于不进行额外的进程
With files
- mv
- cp
mkdir
- All of the above commands can add parameters -v (verbose detail), so that the tool output operation in progress
In addition to mkdir can add -i (interactive interaction), the tool allows you to confirm the request
For help
- man (manual)
- help
- tool -h / --help
$ man cat
# 输出不止一页,所以我们可以控制页面宽度
$ man cat | head -n 20
# 只输出前20行,也可以用fold;将较长的行变为80字符长度
$ man cat | fold
# 使用 help 可以查看 shell内置命令
$ help cat | head -n 20
# 第三方工具可以使用 -h 查看自带的帮助信息
$ java -h