简体   繁体   English

外壳程序脚本中带有/ w参数的sed命令

[英]sed command with /w parameter in shell script

I have a shell script where I am using a sed command with /w flag to create an output file. 我有一个shell脚本,在这里我使用带有/ w标志的sed命令来创建输出文件。 When I try to use that file within the script, I get File not found, but after the script exits, it is created in the directory. 当我尝试在脚本中使用该文件时,找不到文件,但是在脚本退出后,将在目录中创建该文件。

Can I use the file created inside the script? 我可以使用脚本中创建的文件吗? What do I need to do for that to work? 我需要做什么才能使其正常工作?

#!/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

And the output is 输出是

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

After the script finishes, when I look in the directory, the file actually is there. 脚本完成后,当我查看目录时,文件实际上就在这里。 Is there a way I can use it in the zip command? 有什么办法可以在zip命令中使用它吗?

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

Remove space after '${corruptDataFile}' from above line. 从上面的行中删除'${corruptDataFile}'之后'${corruptDataFile}'空格。 It's creating filename with space at end. 它创建的文件名末尾有空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM