Wargames与bash知识01

Wargames与bash知识之level2、level3

Bandit Level 2

这个关卡考的是文件名中有空格的情况,涉及到bash的引用。Bash引用有单引号、双引号和转义。
双引号引用是不完全引用,双引号中有一些特殊符号仍然保持其特殊的作用,如美元符号“$”、反引号“`”、转移符号反斜杠“\”。
单引号引用是完全引用,一切统统打回原形。
转移符号反斜杠“\”只将紧随其后一个字符恢复本意。

bandit2@bandit:~$ ls -l
total 4
-rw-r----- 1 bandit3 bandit2 33 Oct  5 06:19 spaces in this filename
bandit2@bandit:~$ cat "spaces in this filename"  
aBZ0W5EmUfAf7kHTQeOwd8bauFJ2lAiG
bandit2@bandit:~$ cat spaces\ in\ this\ filename
aBZ0W5EmUfAf7kHTQeOwd8bauFJ2lAiG
bandit2@bandit:~$ cat 'spaces in this filename'
aBZ0W5EmUfAf7kHTQeOwd8bauFJ2lAiG
bandit2@bandit:~$

命令说明:
1、cat “spaces in this filename”: 使用双引号注明spaces in this filename是一个有空格字符串,是can命令的一个参数。没有双引号则会被解释为四个参数spaces、in、this、filename。空格是字符串的分割符。字符串分隔符涉及一个环境变量IFS。IFS保存分割符为空格,tab(制表键\t),和newline(换行\n),一般使用第一个作为单词的分隔符。
2、 cat spaces\ in\ this\ filename: 使用转义符“\”将空格恢复为字符意义,去掉特殊含义。
3、 cat ‘spaces in this filename’: 一切回归本意,统统显出原形

PS1:注意使用命令补全tab键,这个bash(GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) )会很贴心的补全相应的引用。Cat s 然后使用tab补全会补全为cat spaces\ in\ this\ filename;cat “s 补全为"spaces in this filename",单引号也一样。
PS2:我在bash(GNU bash,版本 5.0.17(1)-release (x86_64-pc-linux-gnu))试图建一个文件名有空格的文件,建立的有空格文件名会自动多出来一对单引号将文件名括起来。看来不同版本bash对于文件名有空格的问题处理方法是不同的。

gyj@guyanjun:~$ touch "a c v"
gyj@guyanjun:~$ ls
'a c v'

gyj@guyanjun:~$ touch a\ b\ c
gyj@guyanjun:~$ ls
'a b c'

gyj@guyanjun:~$ touch 'a b c'
gyj@guyanjun:~$ ls
'a b c'

Bandit Level 3

这个关卡涉及的问题是点“.”开头的文件,也就是隐藏文件。这个问题很简单,需要注意的ls *也不能将隐藏文件显示出来。要想显示隐藏文件需要使用ls -A 或者ls -a。ls -A选项不会显示当前目录“.”和上级目录(父目录)“…”,而ls -a选项会显示“.”和“…”。

bandit3@bandit:~/inhere$ ls -al
total 12
drwxr-xr-x 2 root    root    4096 Oct  5 06:19 .
drwxr-xr-x 3 root    root    4096 Oct  5 06:19 ..
-rw-r----- 1 bandit4 bandit3   33 Oct  5 06:19 .hidden
bandit3@bandit:~/inhere$ ls -Al
total 4
-rw-r----- 1 bandit4 bandit3 33 Oct  5 06:19 .hidden
bandit3@bandit:~/inhere$ cat .hidden
2EW7BBsr6aMMoJ2HjW067dm8EgX26xNe

你可能感兴趣的:(bash,chrome,开发语言)