-
Notifications
You must be signed in to change notification settings - Fork 0
[系統互動篇] python 與 shell script
Tsung-Jung Tsai (TJ_Tsai) edited this page Nov 13, 2020
·
10 revisions
$ echo '{"profile": {"name": "TJ", "birth": {"year": 2000, "month": 4}}}'
{"profile": {"name": "TJ", "birth": {"year": 2000, "month": 4}}}
使用 python -m json.tool
$ echo '{"profile": {"name": "TJ", "birth": {"year": 2000, "month": 4}}}' | python -m json.tool
{
"profile": {
"birth": {
"month": 4,
"year": 2000
},
"name": "TJ"
}
}
# 從檔案的第一行,讀到的 flag 值
$ flag=' 123 abc \n'
# 檢查長度變化,並執行 strip(),相當於 trim()
$ python3 -c "print(len(\"$flag\"))"
10
$ python3 -c "print(len('$flag'))"
10
$ python3 -c "print( len('$flag'.strip()) )"
7
$ new_flag=```python3 -c "print( '$flag'.strip() )"```
# 檢查長度
$ echo $new_flag
123 abc
$ python3 -c "print(len('$new_flag'))"
7
核心處理
$ flag=' 123 abc \n'
$ new_flag=```python3 -c "print( '$flag'.strip() )"```
- [收錄] How to Execute Shell Commands with Python
- 範例
import os os.system('ls -l') # 無回傳值
import os stream = os.popen('ls -l') # 有回傳值 output = stream.read() print(output) with open('debug.txt', 'w') as f: f.write(output)
tj_tsai / tsungjung411@yahoo.com.tw / tsungjung411@gmail.com