I have the following script to filter an ascii table if one of the fields equals the user input:
ascii () {
arg1=$1
cmd=$(man ascii | awk '/000/,/077/ {print}' | sed '/^$/d')
if [ #$ -eq 0 ]
then
echo "${cmd}"
else
newcmd=$(echo "${cmd}" | awk '{for(i=1;i<NF;i++) if ($i=="${arg1}") {print $0}}')
echo "${newcmd}"
fi
}
needless to say, it's not working but I'm getting the following error:
bash: [: missing `]'
and the colors shown inside the awk string are all purple in my shell instance so I'm assuming the "${arg1}" isnt being interpreted correctly
(i used a var for the first arg the user inputs because awk also uses dollar sign syntax and I didn't know how else to distinguish functions args vs awk fields)
#$
is a comment that starts with$
. Maybe you want$#
– muru Oct 15 '23 at 15:06arg1
isn't going to be expanded in single quotes. You should use an awk variable and set it to the value of the shell variable, like in https://unix.stackexchange.com/a/218773/70524 – muru Oct 15 '23 at 15:08