Search code examples
linuxawksu

AWK error when starting as a particular user


I am using YOCTO Linux and we have only one user 'root' for now. I have a script that runs a process at power ON.

There was a requirement that log folder be created dynamically as per the user and found the solution (from stackoverflow) to run the module as below

/bin/su -c "/usr/local/bin/TestApplication &" - root 

The above works without issues and Folders get created as expected too.

Now when I add 'AWK' command to add date and time to the process output i get error

Command Used

/bin/su -c "/usr/local/bin/TestApplication 2>&1| awk '{ print strftime("%F %T"), $0; fflush(); }' | tee /home/TestApplication.txt &" - root 

Error Message:

awk: cmd. line:1: { print strftime(%F %T),/usr/local/bin/TestApplication_script;fflush();}
awk: cmd. line:1:                  ^ syntax error

No Error if used like this:

/usr/local/bin/TestApplication 2>&1| awk '{print strftime("%F %T"),$0;fflush();}' | tee /home/TestApplication.txt &

So the error only occurs if i try to use '/bin/su -c' please help.


Solution

  • The su invocation removes one layer of shell quoting, and expands the $0. awk is probably invoked with these arguments:

    awk '{ print strftime(%F %T), /bin/sh; fflush(); }'
    

    To prevent that, you need to quote the " and $ characters in the original command line, like this:

    …| awk '{ print strftime(\"%F %T\"), \$0; fflush(); }' |…