$ cat > file1 this is file1 $ cat file1 this is file1 $ cat > file2 this is file2 $ cat file2 this is file2 $ cat file1 file2 > merged_file $ cat merged_file this is file1 this is file2
mv
重新命名檔案
mv origin_name new_name
rm
刪除檔案或資料夾
$ rm file_name
$ rm -r folder_name/
rmdir
刪除資料夾
$ rmdir folder_name/
cp
複製檔案
$ cp oldfile other_folder/newfile
ln
聯結檔案(hard link)
$ touch one $ cat < one $ ln one two $ ls one two $ cat > one this is one $ cat < one this is one $ cat < two this is one
ln -s soft link
hard link 會產生實體檔案,soft link 只是指標的轉向. 如果使用 soft link,當刪除原始檔案時,link 檔案將無法開啟.
ls 最常被使用到的功能還是那個 -l 的選項,為此,很多 distribution 在預設的情況中, 已經將 ll (L 的小寫) 設定成為 ls -l 的意思了!其實,那個功能是 Bash shell 的 alias 功能呢 — 鳥哥的 Linux 私房菜
chmod
修改檔案權限
sh-4.4$ ls -l total 4 -rw-r–r– 1 33581 33581 978 Mar 12 17:30 README.txt -rw-r–r– 1 33581 33581 0 Mar 12 17:32 test sh-4.4$ chmod 777 test sh-4.4$ ls -l total 4 -rw-r–r– 1 33581 33581 978 Mar 12 17:30 README.txt -rwxrwxrwx 1 33581 33581 0 Mar 12 17:32 test sh-4.4$ chmod 444 test sh-4.4$ ls -l total 4 -rw-r–r– 1 33581 33581 978 Mar 12 17:30 README.txt -r–r–r– 1 33581 33581 0 Mar 12 17:32 test
uname
顯示系統相關的資訊
$ uname -a Linux e955582759de 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
選項與參數: -a :所有系統相關的資訊,包括底下的資料都會被列出來; -s :系統核心名稱 -r :核心的版本 -m :本系統的硬體名稱,例如 i686 或 x86_64 等; -p :CPU 的類型,與 -m 類似,只是顯示的是 CPU 的類型! -i :硬體的平台 (ix86) — 鳥哥的 Linux 私房菜
LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]...
DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters Manual page ls(1) line 1 (press h for help or q to quit)
Commands marked with * may be preceded by a number, N. Notes in parentheses indicate the behavior if N is given.
h H Display this help. q :q Q :Q ZZ Exit. ---------------------------------------------------------------------------
MOVING
e ^E j ^N CR * Forward one line (or N lines). y ^Y k ^K ^P * Backward one line (or N lines). f ^F ^V SPACE * Forward one window (or N lines). b ^B ESC-v * Backward one window (or N lines). z * Forward one window (and set window to N). w * Backward one window (and set window to N). ESC-SPACE * Forward one window, but don't stop at end-of-file. d ^D * Forward one half-window (and set half-window to N). u ^U * Backward one half-window (and set half-window to N). ESC-) RightArrow * Left one half screen width (or N positions). ESC-( LeftArrow * Right one half screen width (or N positions). F Forward forever; like "tail -f". r ^R ^L Repaint screen. HELP -- Press RETURN for more, or q when done
按q退出查詢畫面
banner
輸出用#組成的大形文字
實測未出現,上網查了一下 banner 好像有蠻多不同的類型可以安裝?
compress
壓縮檔案
zcat
讀取壓縮檔案
uncompress
解壓縮檔案
compress 已經退流行了。為了支援 windows 常見的 zip,其實 Linux 也早就有 zip 指令了! gzip 是由 GNU 計畫所開發出來的壓縮指令,該指令已經取代了 compress 。 — 鳥哥的 Linux 私房菜
小結
以上是一些基本的 Linux Command , 下一篇,我們會建立.sh 檔,將 Linux Command 依照指定的順序執行 並使用 sh 命令執行 用以完成一些更進階的工作.
For Amazon Linux, the user name is ec2-user. For Centos, the user name is centos. For Debian, the user name is admin or root. For Fedora, the user name is ec2-user. For RHEL, the user name is ec2-user or root. For SUSE, the user name is ec2-user or root. For Ubuntu, the user name is ubuntu or root. Otherwise, if ec2-user and root don’t work, check with your AMI provider.
windows 好像是 Administrator ? 求補充
Docker
安裝 Docker
1
sudo yum install docker
啟動 Docker 服務,並讓它隨系統啟動自動載入
1 2
sudo service docker start sudo chkconfig docker on
We are all aware that null values can be dangerous, if not handled properly. Dereferencing a null-valued variable (i.e. calling a method on it or accessing one of its properties) will result in a NullReferenceException, as demonstrated with the following sample code:
就安全的角度,好像我們要不停的檢查 reference type 是不是 null , 雖然這件事常常發生,好像也很難說成是非預期的行為了… (譯注:又有種中槍的感覺)
To be on the safer side, we should always make sure that reference type values are not null before dereferencing them. Failing to do so could result in an unhandled exception in a specific edge case. Although such a mistake occasionally happens to everyone, we could hardly call it unexpected behavior.
Is it possible to distinguish between a nullable and a non-nullable value type using reflection?
The answer is No.
The same type will be returned for both variables in the above code: System.Int32. This does not mean that reflection has no representation for Nullable, though.
1 2 3
Type intType = typeof(int); Type nullableIntType = typeof(Nullable<int>); bool areTypesEqual = intType == nullableIntType;
上面兩段程式在 runtime 拿到的 type 很不一樣喔, 一個是System.Int32一個是 System.Nullable'1\[System.Int32\]
當 null 遇上多載方法 (Handling Null values in Overloaded methods)
bits 跑到底並不會重頭開始喔,一直移位到爆掉就變 0 了. (這裡會用 32 是因為 int 是 32bit 的數值,你可以試試放超過 32 的數值到 for loop 裡會發生什麼事)
The bits don’t wrap around when they reach the end. That’s why the result of the second expression is 0. The same would happen if we shifted the bit far enough to the left (32 bits because integer is a 32-bit number):
1 2 3 4 5
var shifted = 0b1; for (int i = 0; i < 32; i++) { shifted = shifted << 1; }
However, the bit shifting operators have a second operand. Instead of shifting to the left by 1 bit 32 times, we can shift left by 32 bits and get the same result.
1
var shifted = 0b1 << 32;
Right? Wrong.
The result of this expression will be 1. Why?
Because that’s how the operator is defined. Before applying the operation, the second operand will be normalized to the bit length of the first operand with the modulo operation, i.e. by calculating the remainder of dividing the second operand by the bit length of the first operand.
The first operand in the example we just saw was a 32-bit number, hence: 32 % 32 = 0. Our number will be shifted left by 0 bits. That’s not the same as shifting it left by 1 bit 32 times.
No. The result will be 2 again. By default, the midpoint value will be rounded to the nearest even value. You could provide the second argument to the method to request such behavior explicitly:
1
var rounded = Math.Round(2.5, MidpointRounding.ToEven);
這個行為可以透過MidpointRounding參數改變
1
var rounded = Math.Round(2.5, MidpointRounding.AwayFromZero);
try { var failedInstance = new FailingClass(); } catch (TypeInitializationException) { } Config.ThrowException = false; var instance = new FailingClass();
The static constructor for a class is only called once. If it throws an exception, then this exception will be rethrown whenever you want to create an instance or access the class in any other way.
The class becomes effectively unusable until the process (or the application domain) is restarted. Yes, having even a minuscule chance that the static constructor will throw an exception, is a very bad idea.
var instance = new DerivedClass(); var result = instance.Method(); // -> Method in DerivedClass result = ((BaseClass)instance).Method(); // -> Method in BaseClass // The correct answer is: by using the new modifier.
publicclassBaseClass { publicvirtualstringMethod() { return"Method in BaseClass "; } }
It’s typically used to hide the interface methods from the consumers of the class implementing it, unless they cast the instance to that interface. But it works just as well if we want to have two different implementations of a method inside a single class. It’s difficult to think of a good reason for doing it, though.
var instance = new DerivedClass(); var result = instance.Method(); // -> Method in DerivedClass result = ((IInterface)instance).Method(); // -> Method belonging to IInterface It’s explicitinterfaceimplementation.
publicinterfaceIInterface { stringMethod(); }
publicclassDerivedClass : IInterface { publicstringMethod() { return"Method in DerivedClass"; }
string IInterface.Method() { return"Method belonging to IInterface"; } }
var log = new StringBuilder(); foreach (var number inGetEnumerable(log)) { log.AppendLine($"{number}"); }
不是的, 實際上印出的是
Context created Context disposed 1 2 3 4 5
這點很重要, 因為實務上你很有可能 using dbconnetion 之類的物件, 那麼你在取得真正的資料之前, 你的連線就已經中斷了
This means that in our real world database example, the code would fail – the connection would be closed before the values could be read from the database.
var log = new StringBuilder(); var enumerable = GetCustomEnumerable(log); for (int i = 1; i <= 2; i++) { log.AppendLine($"enumeration #{i}"); foreach (var number in enumerable) { log.AppendLine($"{number}"); } }
enumeration #1 before 1 1 before 2 2 before 3 3 before 4 4 before 5 5 before end enumeration #2 before 1 1 before 2 2 before 3 3 before 4 4 before 5 5 before end
var log = new StringBuilder(); var enumerable = GetCustomEnumerable(log).ToList(); for (int i = 1; i <= 2; i++) { log.AppendLine($"enumeration #{i}"); foreach (var number in enumerable) { log.AppendLine($"{number}"); } }
輸出結果
before 1 before 2 before 3 before 4 before 5 before end enumeration #1 1 2 3 4 5 enumeration #2 1 2 3 4 5