2016-06-30 60 views
0

我有一個shell腳本,我在其中使用帶/ w標誌的sed命令來創建輸出文件。當我嘗試在腳本中使用該文件時,我找不到文件,但在腳本退出後,它將在目錄中創建。在shell腳本中帶/ w參數的sed命令

我可以使用腳本內創建的文件嗎?我需要做些什麼才能工作?

#!/usr/bin/bash 
fileQual=Test_load 

dataFile=$fileQual.dat 
corruptDataFile=$fileQual.err 

cp "${dataFile}" "${dataFile}".cpy 

LANG=C sed -n '/[\x00-\x08\x80-\xFF\x0B-\x0C\x0E-\x1F\x21-\x2B\x2F\x3A-\x40\x5B-\x60\x7B\x7D-\x7F]/w '${corruptDataFile}' ' "${dataFile}".cpy 
echo "file corruptDataFile = ${corruptDataFile}" 
ls ${corruptDataFile} 

function checkNattach() 
{ 
    local fname="$1" 
    local atch 
    if [ -f "$1" ];then 
     atch="-a ${fname} " 
     echo "${fname} exists" 
    else 
     echo "${fname} not found" 
    fi 
    #resFun=atch 
    # echo " in function checkNattach atch $atch " 
    # echo " in function checkNattach resFun $resFun " 
    echo $atch 
} 

TIME=`date +%F-%H%M%S`     # time stamp to the backup file 
FILENAME="Test-$TIME.zip"  # defining the format of the file name while backing up 

echo $FILENAME 
zipincl="" 
zipincl="${zipincl} $(checkNattach ${corruptDataFile})" 
echo "**********zipincl ${zipincl} ${corruptDataFile} " 

zip -9 $FILENAME $corruptDataFile $dataFile 

,輸出是

file corruptDataFile = Test_load.err 

ls: Test_load.err: No such file or directory 


Test-2016-06-29-204904.zip 

**********zipincl Test_load.err not found Test_load.err 
+ zip -9 Test-2016-06-29-204904.zip Test_load.err Test_load.dat 
     zip warning: name not matched: Test_load.err 

劇本完成後,當我看到在目錄中,該文件實際上是存在的。有沒有一種方法可以在zip命令中使用它?

+0

你想用'\ x00- \ x08 \ x80..'完成什麼? – sjsam

+0

基本上,刪除文件中包含這些字符的行 – adbdkb

+0

這是很多代碼。你可以在[MCVE](http://stackoverflow.com/help/mcve)中隔離你的問題嗎?另外,你正在運行什麼操作系統? '/ usr/bin/bash'似乎是一個奇怪的地方,操作系統會影響你正在運行的其他工具的風格,比如'sed'。噢,作爲一般規則,總是在bash中引用變量。 'dataFile =「$ fileQual.dat」'等 – ghoti

回答

1
LANG=C sed -n '/[\x00-\x08\x80-\xFF\x0B-\x0C\x0E-\x1F\x21-\x2B\x2F\x3A-\x40\x5B-\x60\x7B\x7D-\x7F]/w '${corruptDataFile}' ' "${dataFile}".cpy 

從上方移除'${corruptDataFile}'後的空間。它在最後創建帶空格的文件名。

+0

這似乎是原因。我會在這裏進行更多的測試並報告。 – adbdkb

+0

我執行了一些更多的測試,似乎工作 – adbdkb