Shell学习笔记
导语
Shell是UNIX操作系统的命令语言。
它既是用户与UNIX操作系统对话的语言,也可以作为程序设计的语言,所以掌握这项语言是十分有意义的。
本文是我学习shell时记录的笔记。
1 | [Chen9@localhost ~]$ |
It tells you that you are on the machine Chen9
and your current working directory is ~
(short for “home”). The $
tells you that you are not the root user.
1 | [Chen9@localhost ~]$ date |
The date
prints the current date and time.
1 | [Chen9@localhost ~]$ echo hello |
The echo
with the argument can print out its argument.
If you want to provide an argument that contains spaces of other special characters, you can quote the argument with '
or "
, or use \
before the space.
1 | [Chen9@localhost ~]$ echo $PATH |
When we run a command, the shell will search through the :
-separated list of directories in $PATH
for a file by that name.
The which
program can find out which file is executed for a given program name.
If you give the path to the file we want to execute, it can bypass $PATH
.
1 | [Chen9@localhost ~]$ pwd |
The pwd
can show you the current working directory and change with the cd
command.
The .
refers to the current directory, and ..
to its parent directory.
1 | [Chen9@localhost ~]$ ls |
The ls
will print the contents of the current directory.
1 | [Chen9@localhost ~]$ ls -l /home |
The -l
will use a long listing format.
The d
tells us that chen9
(at the end of that line) is a directory.
Then follow three groups of three characters(rwx
) indicate what permssion the owner of the file(chen9
), the owning group(chen9
), and everyone else have. -
indicates that the given principal does not have the given permission.
The 4096
means the size of this file.
You can use mv
to rename/move a file, cp
to copy a file, mkdir
to make a new directory.
1 | [Chen9@localhost ~]$ man ls |
The man
program can show you a program’s arguments, inputs, outputs, or how it works in a manual page. Press q
to exit.
1 | [Chen9@localhost ~]$ echo hello > hello.txt |
You can use < file
and > file
to rewire the input and output streams of a program to a file respectively.
You can also use >>
to append to a file.
1 | [Chen9@localhost ~]$ ls -l / | tail -n1 |
The |
lets you chain programs such that the output of the former is the input of the latter.
1 | [Chen9@localhost ~]$ find -L /sys/class/backlight -maxdepth 2 -name '*brightness*' |
The sudo
can let you do something as “su”(short for super user).
The /sys
exposes a number of kernel parameters as files, you can reconfigure the kernel easily.
1 | [Chen9@localhost ~]$ sudo echo 3 > brightness |
Operations like |
, >
, and <
are done by the shell, so the shell(which is authenticated just as your user) tries to open the brightness file for writing will be rejected.
You can work around this:
1 | [Chen9@localhost ~]$ echo 3 | sudo tee brightness |
The tee
will print contents both to file and your terminal.
1 | [Chen9@localhost ~]$ foo=bar |
The "
delimiter will substitute variable values, but '
will not.
1 | mcd () { |
An example of a function that create a directory and cd
s into it.
$0
-Name of the script.$1
to$9
-Arguments to the script.$1
is the first argument and so on.$@
-All the arguments.$#
-Number of the arguments.$?
-[[#^d5548a|Return code]] of the previous command.$$
-Process identification number(PID) for the current script.!!
-Entire last command, including arguments. You can re-execute the command with sudo by doingsudo !!
.$_
-Last argument from the last command. You can also quickly get this value by typingEsc
+.
orAlt
+.
Return Code’s value of 0 usually means everything went OK; anything different from 0 means an error occurred.
1 | [Chen9@localhost ~]$ false || echo "Oops, fail" |
The &&
and ||
are short-circuiting operators. Command can also be separated within the same line using a semicolon ;
. true
will have a 0 return code and the false
command will have a 1 return code.
1 | !/bin/bash |
1 | [Chen9@localhost ~]$ ./scripttest.sh |
command substitution: The $(CMD)
command will execute CMD
, get the output of the command and substitute it in place.
process substitution: The <(CMD)
command will execute CMD
and place the output in a temporary file and substitute the <()
with that file’s name.
1 | convert image.{png,jpg} |
- Wildcards - use
?
to match one character and*
to match any amount of characters. - Curly braces - use
{}
whenever you have a common substring in a series of commands, it can expand this automatically.
1 | #!/user/local/bin/python |
Scripts need not nevessarily be written in bash to be called from the terminal.
A shebang line at the top of the script will let the kernel knows to execute this script with a python interpreter.
The env
will make use of the PATH
environment variable, so shebang line would look like #!/usr/bin/env python
.
1 | Find all directories named src |
The find
will recursively search for files matching some criteria.