1. 程式人生 > >shell提取檔案行的內容並執行

shell提取檔案行的內容並執行

需求:需要大批量的移除資料夾中的某一類語言檔案,希望能批量執行

檔案german.txt部分內容如下:

"/Library/Application Support/Apple/BezelServices/AppleBluetoothHIDKeyboard.plugin/Contents/Resources/de.lproj" 
couldn’t be removed, because you didn't authorize this operation."
/Library/Application Support/Apple/BezelServices/AppleBluetoothHIDMouse.plugin/Contents/Resources/de.lproj" 
couldn’t be removed, because you didn't authorize this operation.
"/Library/Application Support/Apple/BezelServices/AppleBluetoothMultitouch.plugin/Contents/Resources/German.lproj" couldn’t be removed, because you didn't authorize this operation.
"/Library/Application Support/Apple/BezelServices/AppleHSBluetooth.plugin/Contents/Resources/de.lproj" couldn’t be removed, because you didn't authorize this operation.
"/Library/Application Support/Apple/BezelServices/AppleTopCaseHIDEventDriver.plugin/Contents/Resources/de.lproj" couldn’t be removed, because you didn't authorize this operation.
"/Library/Application Support/Apple/BezelServices/IOAppleBluetoothHIDDriver.plugin/Contents/Resources/de.lproj" couldn’t be removed, because you didn't authorize this operation.
"/Library/Application Support/Apple/BezelServices/IOBluetoothHIDDriverGeneric.plugin/Contents/Resources/de.lproj" couldn’t be removed, because you didn't authorize this operation.
........

1)獲取對應路徑,過濾路徑外的關鍵詞句子

#!/bin/bash

cat $1 |grep Library> localiation_path.txt    #$1 表示執行指令碼時,後面跟的文字
awk 'BEGIN {FS="\""}{print $2}' localiation_path.txt > localiation_final.txt

2)迴圈讀取和刪除對應的內容

#!/bin/bash


num=0
cat $1|while read line    #$1 表示執行指令碼時,後面跟的文字
do
	rm -rf "$line"
	let num+=1
	echo $num
done 

"$line"   #需要用引號包含,才能解決路徑中包含空格時不能執行命令的問題
 讀取過程的坑      # 被處理的文字中最後一行內容一定要包含換行符,否則有可能讀出來少一行。

附:文字german.txt來源的語言搜尋命令

#!/bin/bash

sudo find /Library -name German.lproj > find_loc.txt

sudo find /Library -name de.lproj >> find_loc.txt
 
cat find_loc.txt |sort > find_final.txt