Shell
1. ~$ --- waiting for input, ~ is the home directory
~# --- superuser
root --- the program 'root' is currently not installed
apt-get install root-system-bin
2. the accelerate key is so rubbish which same as Emacs
3. Quoting reserved Characters
after quoting the char will be interpreted literally, and not for any reserved meaning
the newline is a exception
1) use backslashes /
--- this is cumbersome since there will be many backslashes
e.g
echo /"It isn/'t nice/!/"
echo /$HOSTNAME is nice/!
2) single quote characters (')
---this is the simplist way
---newline also can be quoted by this method
e.g
echo ' * ! " / /'
3) use ""
---the arguments will keep the remaining mean
4) use $'string'
string = /a /b /e /f /n /r /t /v //
/NNN Character whose ascii code is NNN in octal
e.g
echo $'/266'
4. Running a list of cmds
1) use semicolon(;) to seperate them
e.g
$ clear; logout
$ hostname;hostname;hostname
if it is too long, just use /
2) batch cmd file
5. Running One Command and Then Another
&&
e.g
$foo && bar
---To run foo, and then run bar only if foo exists and successfully ran
grep -i planning operations && less operations
Question:
1) what is different from "4.Running a list of cmds"
2) What is different from Pipe
6. Running One Command or Another
||
e.g
$w | who
7. =====Automatically Answering a Command Prompt====
pipe the output of yes to it
$ yes n | mv *.samples live
$ yes 'whoami' | head -5 | farboo
8. Specifying the Output of a Command as an Argument???
9. Redirecting Input and Output
a) default standard Input is keyboard, so when we cin it will wait for inputs from keyboard but not other device
b) default standard Output is the screen on your terminal, wo cout will output the string to the screen but not other device
c) you redirect these streams—to a file, or even another command—with redirection
d) The > operator overwrites a file with output if the file already exists, whereas the >> operator will append output to the file.
e) To redirect both standard output and standard error to the same file, use &> instead of the stdout and stderr operators. [there is no &>>]
f) Pipe
g)
e.g
1>/dev/null 2>&1 &的含义
shell中可能经常能看到:>/dev/null 2>&1
命令的结果可以通过%>的形式来定义输出
/dev/null 代表空设备文件
> 代表重定向到哪里,例如:echo "123" > /home/123.txt
1 表示stdout标准输出,系统默认值是1,所以">/dev/null"等同于"1>/dev/null"
2 表示stderr标准错误
& 表示等同于的意思,2>&1,表示2的输出重定向等同于1
那么本文标题的语句:
1>/dev/null 首先表示标准输出重定向到空设备文件,也就是不输出任何信息到终端,说白了就是不显示任何信息。
2>&1 接着,标准错误输出重定向等同于 标准输出,因为之前标准输出已经重定向到了空设备文件,
所以标准错误输出也重定向到空设备文件。
最后的&表示后台执行,你可以继续占有你的输入窗口
10. Jobs
1) jobs that arereading standard input and writing standard output are the foreground jobs,
while any other jobs are said to be running in the background.
2) The shell assigns each job a unique job number. You can use it as an argument to specify the job to commands.
Do this by preceding the job number with a percent sign (%).
3) ctrl+z to suspend a job
4) Putting a Job in the Background &
apropos shell > shell-commands &
then use jobs cmd to check the job status
5) bg --- To move a job from the foreground to the background, first suspend it and then type bg,
bg %4 for more suspend jobs
6) fg
7) jobs ---list jobs
8) ctrl + c ---stop a fg job
kill ---stop a bg job
11. Command histroy is stored in a file in the home dir which named .bash_history
1) history --view the history file
2) !1 --- To run history event number 1
12. Using Shell Variables
1) case sensitive
2) built-in variables HOME
3) assigning a variable
NAME='Variable'
Be sure not to type any space characters on either side of the equals sign.
4) NAME='whoami'
$NAME ---sx
echo $NAME ---whoami ---to display the content of the variable
echo ${NAME}now ---whoaminow
echo $NAMEnow ---referencing a variable called NAMEnow
5) Removing a Variable
a) NAME= ---remove content of a veriable
b) unset NAME --- remove the variable itself
6) Listing Variables
set
7) PS1 ---Changing the Shell Prompt
8) PATH, .bashrc
9) echo $SECONDS ---Seeing How Long Your Shell Has Been Running
10) alias ---same as variable
11) script
12) Using Shell Startup Files
/etc/profile
---This is a generic, systemwide startup file that is run for all users; only the system administrator can add,
delete, or change commands in this file.
.bash_profile
---Bash reads and executes the commands in it
run one time when login
.bashrc
---Commands in this file run whenever a new shell is started except for the login shell.
run both at log in and for all subsequent shells
.bash_logout
13) script ---capture the shell logs
script in another script session will capture Russian dolls logs
---Russian dolls is a kind of toys with the same doll in different sizes.
It can open in half, and you can put the smaller one into the bigger one
14) Running other shells
bash ---This will suspend your current shell and run the new shell;
when you exit the new shell, you will return to your old shell.
exec bash ---To run a shell in place of your current shell, use exec.
15) exit or ctrl+d ---Exiting a Shell
16) echo $0 ---Getting the Name of Your Current Shell
17) chsh ---Changing Your Default Shell