0

I have two pieces of code that work independently, but not together. I need to put the if statement inside the <Command_To_Be_Ran>. Any ideas to how i can get this to work?

for i in t{1..2}; do ssh -qex -t $i 'echo <Sudo_Password> | sudo -S <Command_To_Be_Ran>' done 

and

if (( $(grep -o "fips=1" /etc/default/grub | wc -l) > 1 )); then sed -i 's/ fips=1//' /etc/default/grub fi 
2
  • See stackoverflow.com/a/3872762/1745001
    – Ed Morton
    CommentedAug 22, 2020 at 0:52
  • FYI it should be possible to simplify the conditional to if grep -q "fips=1" /etc/default/grub, which uses the grep exit status instead of counting matched lines with wcCommentedAug 22, 2020 at 1:12

1 Answer 1

0

I think now it will work :D

for i in t{1..2}; do ssh -qex -t $i "echo <Sudo_Password> | sudo -S bash -c '\ if (( $(grep -o 'fips=1' /etc/default/grub | wc -l) > 1 )); \ then sed -i 's/ fips=1//' /etc/default/grub ;\ fi'" done 

should work, the line breaks can be replaced with ; and bash -c and the double quotes (") enable you to run a command like from within a shell.

If you have zsh installed (or even in some versions of bash) you could probably use eval instead of bash -c

5
  • Trying to get this to work but there is a single quote right before the echo on the sudo password. This makes it get ingested incorrectly stopping at the first single quote at the fips=1... atleast i believe. Testing now.
    – TrevorKS
    CommentedAug 21, 2020 at 18:38
  • now it should work
    – pt1997
    CommentedAug 21, 2020 at 19:24
  • You should create a local script and feed it to you ssh session, it will be much easier to execute and write. As far as the bash goes you can use the -- trick.
    – nethero
    CommentedAug 21, 2020 at 20:05
  • i would agree, but i assume he wants to run the command on multiple systems, hence the for loop
    – pt1997
    CommentedAug 21, 2020 at 20:10
  • I think Ed Morton is right, that's probably a better solution: stackoverflow.com/a/3872762/1745001
    – pt1997
    CommentedAug 22, 2020 at 8:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.