i have a problem with the following command. I want to write a script that sets the vncpasswd for another user. If I run the following command it will only work for the current user.#< echo passw0rd\npassw0rd\nn | vncpasswdBut when running the next command, i get a problem because the resulting echo is not a valid input for the programm anymore.#< su nonrootuser -c echo passw0rd\npassw0rd\nn | vncpasswd#> Using password file /home/nonrootuser/.vnc/passwd#> Password: Warning: password truncated to the length of 8.#> Verify:   Passwords do not match. Please try again.Thanks for your answers! :D
Automate commandline application for another user
shell script;echo
Depending on the shell, the echo command may or may not perform backslash expansion to turn \n into a newline. For example, bash does not by default, but does after shopt -s xpg_echo.But even if the echo builtin of sh performs backslash expansion on your machine, your code won't trigger that because of how you wrote the command. You're passing the string echo passw0rdnpassw0rdnn | vncpasswd to the su command: the second double quote terminates the double-quoted string literal and the third double quote starts a new double-quoted string literal, so the \n sequences are not in double quotes in the outer shell and so \n expands to n. You meant to write something likesu nonrootuser -c echo -e 'passw0rd\npassw0rd\nn' | vncpasswdso that the backslashes are passed literally through both shells, and with the -e option passed to echo to tell it to perform backslash expansion.But rather than fiddle with echo in a not entirely portable way, just call printf (which is fully reliable) or run echo once for each line:su nonrootuser -c { echo 'passw0rd'; echo 'passw0rd'; echo n; } | vncpasswdsu nonrootuser -c printf '%s\n' 'passw0rd' 'passw0rd' n | vncpasswdsu nonrootuser -c   vncpasswd <<EOFpassw0rdpassw0rdnEOF