PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to avoid the command execution when appending lines to a file

 May 12, 2022     append, linux, tee     No comments   

Issue

I'm trying to save the content of script into a file using command line, but I noticed that when the tee command detects linux commands such as $(/usr/bin/id -u), it execute the commands rather than saving the lines as it is. How to avoid the execution of the commands and saving the text exactly as I entered it?

   $tee -a test.sh << EOF 
   if [[ $(/usr/bin/id -u) -ne 0 ]]; then
       echo You are not running as the root user.
       exit 1;
   fi;
   EOF
   if [[ 502 -ne 0 ]]; then
       echo You are not running as the root user. 
       exit 1;
   fi;

Complete script contains many more lines, but I chose /usr/bin/id -u as a sample.


Solution

This has nothing to do with tee or appending to the file, it's how here-documents work. Normally variable expansion and command substitution is done in them.

Put single quotes around the EOF marker. This will treat the here-document like a single-quoted string, so that $ will not expand variables or execute command substitutions.

tee -a test.sh << 'EOF'
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
   echo You are not running as the root user.
   exit 1;
fi;
EOF
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
   echo You are not running as the root user. 
   exit 1;
fi;


Answered By - Barmar
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing