1

Based on:

https://unix.stackexchange.com/questions/1459/remote-for-loop-over-ssh

I am trying to run a for loop command through oc rsh but it fails with below error:

$ oc rsh my-app-10-lprki 'for i in /var/lib/*; do echo $i; done'
exec failed: container_linux.go:345: starting container process caused "exec: \"for i in /var/lib/*; do echo $i; done\": stat for i in /var/lib/*; do echo $i; done: no such file or directory"
command terminated with exit code 1

I have also tried without quotes, with double quotes but that also fails.

This works fine (without quotes):

$ oc rsh my-app-10-lprki ls -la /var/lib/
total 24
drwxr-xr-x.  1 root       root   104 Nov 22 22:21 .
drwxr-xr-x.  1 root       root    17 Sep 28  2017 ..
drwxr-xr-x.  2 root       root    44 Sep 28  2017 alternatives

Any suggestions on how to run a for loop remotely through oc rsh like (that currently fails):

oc rsh my-app-10-lprki 'for i in /var/lib/*; do echo $i; done'

?

Based on below suggestions I have also tried:

$ oc rsh my-app-10-lprki -- /bin/sh  -c  'for i in `ls  /var/lib`; do echo $i; done'
exec failed: container_linux.go:345: starting container process caused "exec: \"--\": executable file not found in $PATH"
command terminated with exit code 1

and:

$ oc rsh my-app-10-lprki -- /bin/bash  -c  'for i in `ls  /var/lib`; do echo $i; done'
exec failed: container_linux.go:345: starting container process caused "exec: \"--\": executable file not found in $PATH"
command terminated with exit code 1

and:

$ oc rsh my-app-10-lprki -- /usr/bin/bash  -c  'for i in `ls  /var/lib`; do echo $i; done'
exec failed: container_linux.go:345: starting container process caused "exec: \"--\": executable file not found in $PATH"
command terminated with exit code 1

Where:

$ oc rsh my-app-10-lprki which bash
/usr/bin/bash
u123
  • 15,603
  • 58
  • 186
  • 303

2 Answers2

4

This did the trick:

oc exec my-app-10-lprki -- bash -c 'for i in /var/lib/; do echo $i; done'
u123
  • 15,603
  • 58
  • 186
  • 303
1

Assuming /bin/sh the shell in your container

oc rsh my-app-10-lprki -- /bin/sh  -c  'for i in `ls  /var/lib`; do echo $i; done'

will list out the files in /var/lib directory

here is the example of mysql pod

[suresh.vishnoi@blah ~]$ oc exec  mysql-4-lnlpx --  /bin/sh  -c  'for i in `ls  /var/lib`; do echo $i; done'
alternatives
dbus
games
initramfs
machines
misc
mysql
rhsm
rpm
rpm-state
selinux
systemd
yum
Suresh Vishnoi
  • 17,341
  • 8
  • 47
  • 55