$ 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
2. 建立 IAM User 與指定 Group 權限 AWS IAM 的權限觀念是透過 User 與 Group 來組合的, 權限是授與 Group , 而 User 隸屬於 Group 便擁有其權限, 同時 AWS 提供多組(347 組)預設的 Policies, 讓人選擇 當然也可以建立自已的 Policy. *不確定有沒有反向的 Policy , 如果有當不同的 Group Policy 有衝突時該如何處理。
建立使用者時, 使用 AutoGenerated Password 時 要記得取得 password 在最後一步會按下 Show 就會顯示 3. 設定 AWS Account ID 與 Alias
Q & A
如何禁用 Root user 登入 ? 可以停用而不刪除一個 user account 嗎?
1 2 3 4 5 6 7
root account 無法停用 IAM User 可以透過 disable passwd 方式停用 root account 基本的 practice 1. 啟用 MFA 2. 移除 Access Credential 概念就跟 Windows Administrator or Linux root 一樣 需要時再用
滾動部署策略是指通過逐個替換應用的所有例項, 來緩慢釋出應用的一個新版本。 通常過程如下: 在負載排程後有個版本 A 的應用例項池, 一個版本 B 的例項部署成功,可以響應請求時, 該例項被加入到池中。 然後版本 A 的一個例項從池中刪除並下線。 考慮到滾動部署依賴於系統, 可以調整如下引數來增加部署時間:
並行數,最大批量執行數:同時釋出例項的數目
最大峰值:考慮到當前例項數,例項可以加入的數目
最大不可用數:在滾動更新過程中不可用的例項數
優點:
便於設定
版本在例項間緩慢釋出
對於能夠處理資料重平衡的有狀態應用非常方便
缺點:
釋出/回滾耗時
支援多個 API 很困難
無法控制流量
藍綠部署
藍綠部署策略與滾動部署不同, 版本 B(綠)同等數量的被並排部署在版本 A(藍)旁邊。 當新版本滿足上線條件的測試後, 流量在負載均衡層從版本 A 切換到版本 B。