-1

Thanks for looking into my concern.

I have a piece of code inside my shell script as shown below and want to redirect logs of below script to a file. could you please help with it.

ssh -i /home/$cu/.ssh/${ssh_key}_rsa $Linux_User@$Linux_Machine <<EOF
{
source $HOME/.bashrc >/dev/null
chmod 777 -R ${Target_Dir}/Scripts/ >/dev/null
sh ${Target_Dir}/Scripts/tomcat.sh ${Target_Dir}
source $HOME/.bashrc >/dev/null
sh ${Target_Dir}/Scripts/appdeploy-multi.sh ${Target_Dir} $APP_VERSION $APP_NAME
sh ${Target_Dir}/Scripts/check-status.sh $APP_NAME
rm -rf ${Target_Dir}/
}
EOF
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Ras
  • 543
  • 1
  • 9
  • 25
  • 2
    Append `> file.log` to first line? – Cyrus Aug 11 '17 at 19:43
  • Can you show what you have attempted and an example of what you expect to see? – Mad Physicist Aug 11 '17 at 19:43
  • https://unix.stackexchange.com/q/88490/79307 – Mad Physicist Aug 11 '17 at 19:46
  • 2
    `ssh ... < file.log` – janos Aug 11 '17 at 19:49
  • See: [How can I write a heredoc to a file in Bash script?](https://stackoverflow.com/q/2953081/3776858) – Cyrus Aug 11 '17 at 19:56
  • BTW, `${foo}` is no more correct than `$foo` is (unless suffixed by characters valid in variable names), whereas `"$foo"` fixes real bugs (which you'd see in practice if, say, your directory name had spaces). Though passing a script to a shell via an unquoted heredoc means you've got those bugs regardless, unless the content is quoted in an `eval`-safe manner. So, for instance, `printf -v q_Target_dir '%q' "$Target_dir"` would make it safe to use `$q_Target_dir` or `${q_Target_dir}` is your heredoc. – Charles Duffy Aug 11 '17 at 20:01
  • (the set of issues avoided by the above includes security vulnerabilities -- if the original code was run with `APP_VERSION='3.5$(rm -rf ~)'`, you'd have a bad day; whereas with `printf -v q_APP_VERSION %q "$APP_VERSION"` and the heredoc expanding only `$q_APP_VERSION`, you're safe). – Charles Duffy Aug 11 '17 at 20:05

1 Answers1

0

@cyrus/janos: Thank you so much. it fixed the thing.

ssh ... <<EOF > file.log
Ras
  • 543
  • 1
  • 9
  • 25