今天來看到下一個room
Windows command line / powershell
基本上也是來介紹終端的指令然後讓你實作那些指令會出現什麼
上面兩個過後還會介紹linux shell
$tasklist
$tasklist /FI "imagename eq sshd.exe"
$tasklist /?
$taskkill /PID 4567
$taskkill /IM notepad.exe
$taskkill /F /PID [pid_number]
$taskkill /F /IM [process_name]
powershell在指令上會跟cmd有所不同
| 功能 | CMD 指令 | PowerShell 指令 (正式寫法) | PowerShell 簡寫 (別名) |
| ------ | -------------------- | ------------------------------ | ------------------- |
| 查看目錄內容 |dir
|Get-ChildItem
|ls
,dir
,gci
|
| 顯示目前路徑 |cd
|Get-Location
|pwd
|
| 切換目錄 |cd <目錄>
|Set-Location <目錄>
|cd
,sl
|
| 建立目錄 |mkdir <目錄>
|New-Item -ItemType Directory
|mkdir
,ni
|
| 刪除檔案 |del <檔案>
|Remove-Item <檔案>
|del
,rm
,ri
|
| 複製檔案 |copy a.txt b.txt
|Copy-Item a.txt b.txt
|copy
,cp
,ci
|
| 移動檔案 |move a.txt C:\test
|Move-Item a.txt C:\test
|move
,mv
,mi
|
| 查看檔案內容 |type a.txt
|Get-Content a.txt
|gc
,cat
,type
|
| 列出進程 |tasklist
|Get-Process
|gps
,ps
|
| 終止進程 |taskkill /PID 1234
|Stop-Process -Id 1234
|kill
,spps
|
| 查看服務 |sc query
|Get-Service
| 無 |
| 系統資訊 |systeminfo
|Get-ComputerInfo
| 無 |
| 測試連線 |ping google.com
|Test-Connection google.com
| 無 |
Shell是一種讓使用者可以和作業系統Kernel(核心用來控制CPU、記憶體、硬碟等硬體)互動溝通的橋樑
常見的指令之前也已經提過的這邊就不在重複
這裡直接用shell script來講
#!/bin/bash
# 這是一個簡單的 Shell Script 範例
# 印出問候語
echo "Hello, Linux!"
# 宣告變數
USER_NAME="Alice"
echo "目前使用者是:$USER_NAME"
# 條件判斷
if [ -d "/etc" ]; then
echo "/etc 資料夾存在"
else
echo "/etc 資料夾不存在"
fi
# 迴圈
for i in {1..3}; do
echo "第 $i 次迴圈"
done
nano hello.sh
chmod +x hello.sh
./hello.sh
Shell Script的運用範圍很廣,自動備份、批量修改檔案、監控系統資訊、定時任務....
房間內也有簡單的任務讓我寫一個shell去執行結果
Shell Script 在自動化任務上有很大的作用,可節省大量手動操作的時間
在未來的程式開發與系統管理中,Shell Script 都是一項非常實用的技能,對於提升工作效率與自動化能力都有很大的幫助