adbdkb adbdkb - 5 months ago 10
Bash Question

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. When I try to use that file within the script, I get File not found, but after the script exists, 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?

Answer
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. It's creating filename with space at end.