Using SNAP command - how to force messaging to log file?

If I enter the command

snap list junk

the error message reported is

error: snap "junk" not found

I expect the following to correctly capture any such message in a specified file:

snap list junk 2>&1 >msg_file.txt

That does not work. I’ve tried wrapping the snap command within braces, which I’ve had to do for some cases, but that does not work for this scenario.

Any suggestions?

Test script
#!/bin/sh

tmp="/tmp/$(basename "${0}" ".sh")__$$.tmp" ; rm -f "${tmp}"

#for packageName in junk software-boutique
for packageName in junk
do
	skip=0

	export packageName
	rm -f "${tmp}"
	#snap list "${packageName}" 2>&1 >"${tmp}"
	#{ snap list "${packageName}" 2>&1 } >"${tmp}"
	testor=$( snap list "${packageName}" 2>&1 )

	#testor=$( grep 'error: no matching snaps installed' "${tmp}" )
	#testor=$( grep 'error: snap' "${tmp}" | grep 'not found' )
	#testor=$( grep 'error: snap' "${tmp}" )
	testor=$( echo "${testor}" | grep 'error: snap' )

echo "testor = '$testor'"

	if [ -n "${testor}" ]
	then
		echo "\n No SNAP package by name of '${packageName}' ..."
	else
		if [ -n "${packageName}" ]
		then
			rm -f "${tmp}"
			snap connections "${packageName}" >"${tmp}"

			if [ -s "${tmp}" ]
			then
				echo "\n Found connections related to package '${packageName}':\n"
				cat "${tmp}"
				echo "\n Skip or Continue ? [S/c] => \c" ; read dowhat
				if [ -z "${dowhat}" ] ; then  dowhat="A" ; fi
				case "${dowhat}" in
					[Ss] ) echo "\t Purge of '${packageName}' deferred ...\n" ; skip=1 ;;
					* ) echo " Ignoring status of connections ..." ;;
				esac
			else
				echo " Found no connections related to package.  Proceeding with purge ...\n"
			fi

			if [ ${skip} -eq 0 ]
			then
				echo "to purge"
				#snap remove --purge "${packageName}"
				echo ""
				#snap list "${packagName}"
			fi
		fi
	fi

	echo "\n Hit return to continue with next package ..." ; read k
done
1 Like