ComplianceAsCode/content

View on GitHub
tests/ds_unselect_rules.sh

Summary

Maintainability
Test Coverage
#!/bin/bash
 
if [ ! -f "$1" ] || [ ! -f "$2" ]; then
Double quote to prevent globbing and word splitting.
Don't use variables in the printf format string. Use printf "..%s.." "$foo".
printf "Usage: $(basename $0) <DATASTREAM> <UNSELECT_LIST_FILE>
Copies the DATASTREAM file to /tmp and unselects the rules listed
in the UNSELECT_LIST_FILE from the /tmp/DATASTREAM file.\n"
exit 1
fi
DS=$1
UNSELECT_LIST=$2
 
 
printf 'Copying %s file to /tmp\n' "$DS"
Double quote to prevent globbing and word splitting.
cp $DS /tmp || exit 1
Double quote to prevent globbing and word splitting.
DS="/tmp/$(basename $DS)"
 
printf 'Unselecting rules listed in the %s from the %s\n' "$UNSELECT_LIST" "$DS"
Double quote to prevent globbing and word splitting.
To read lines rather than words, pipe/redirect to a 'while read' loop.
for rule in $(cat $UNSELECT_LIST); do
Double quote to prevent globbing and word splitting.
sed -i "/<.*Rule.*id=\"$rule/s/selected=\"true\"/selected=\"false\"/g" $DS || exit 1
Double quote to prevent globbing and word splitting.
sed -i "/<.*select.*idref=\"$rule/s/selected=\"true\"/selected=\"false\"/g" $DS || exit 1
done
printf "Done\n"