Ubuntu: Create a file and write to it [closed]



Question:

Trying to create a file auto.home under /etc and write this to the file:

192.168.56.101:/NFS/home/&  

I am not doing it with commandline, but bash script.

Tried this but didnt work: cat 192.168.56.101:/NFS/Home/& > /etc/auto.home


Solution:1

You can use "EOL" to indicate when you want to end writing to your file using cat as follows:

#!/usr/bin/env bash  cat > temp_file << EOL  192.168.56.101:/NFS/home/&  EOL  sudo mv temp_file /etc/<filename>  

This would keep writing to temp_file, until it gets "EOL" meaning End of Line.


Refered from: A similar question on SO


Solution:2

You can't use sudo for redirection, the command will be run with root privileges but the > (redirection) will not so you'll get a "Permission denied" error. Using here-docs as Jobin's answer does will not help.

Instead, you can use one of these approaches:

echo "192.168.56.101:/NFS/home/&" | sudo tee -a /etc/auto.home  

tee lets you append (-a) to a file, since you're running it with sudo it will work.

Alternatively, use a temp file and move it:

echo "192.168.56.101:/NFS/home/&" > home.auto && sudo mv home.auto /etc  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »