I'm trying to:
- Accept input in bash and store it verbatim to a file
- cat the contents of the file.
Any special characters should also be written verbatim to the file.
Sample Input
./verbatim.sh -a smb 'ngrep -i -d $INTERFACE 's.?a.?m.?b.?a.*[[:digit:]]''
Expected output
Inner single quotes should remain
-a smb 'ngrep -i -d $INTERFACE 's.?a.?m.?b.?a.*[[:digit:]]''
Actual output
Inner quotes are stripped
-a smb 'ngrep -i -d $INTERFACE s.?a.?m.?b.?a.*[[:digit:]]'
Code
#!/bin/bash
#
# Script Name: verbatim.sh
chars='[ !"#$&()*,;<>?\^`{|}]'
for arg
do
if [[ $arg == *\'* ]]
then
arg=\""$arg"\"
elif [[ $arg == *$chars* ]]
then
arg="'$arg'"
fi
allargs+=("$arg") # ${allargs[@]} is to be used only for printing
done
printf '%s\n' "${allargs[*]}" > /tmp/pse.tmp
cat /tmp/pse.tmp
I'm not sure if bash can accomplish what I am asking or not?
Would this be better to accept using read or IFS? The goal of my script is to append the user's input to a file that will be used as a "quick reference" for a particular service.
printf %q
or"${var@Q}"
instead of rolling your own quoting – muru May 28 '20 at 02:40'ngrep -i -d $INTERFACE 's.?a.?m.?b.?a.*[[:digit:]]''
, you have one single-quoted string'ngrep -i -d $INTERFACE '
followed by unquoteds.?a.?m.?b.?a.*[[:digit:]]
, followed by the single-quoted empty string''
. – muru May 28 '20 at 03:32