0

Script as it is so far (not working):

#!/bin/bash
while read line
uname="cat /etc/passwd | grep bash | sed 's/:.*//'"
echo "config system admin 
for each line $uname >
if $uname = "root" then
echo "skipping $uname 'root'" else
echo "edit '$uname'/n
set remote-auth enable/n
set trusthost1 8.8.8.8 255.255.255.255/n
set accprofile "admin"/n
set vdom "root"/n
set remote-group "foobar"/n
set password ENC potatoes/n
next/n" > output.txt

Output from "cat /etc/passwd | grep bash | sed 's/:.*//'"

foo
root
bar

Expected output in "output.txt" with three users listed in "/etc/password" being foo, root, and bar:

config system admin 
edit foo 
set remote-auth enable
set trusthost1 8.8.8.8 255.255.255.255
set accprofile "admin"
set vdom "root"
set remote-group "foobar"
set password ENC potatoes
next
edit bar
set remote-auth enable
set trusthost1 8.8.8.8 255.255.255.255
set accprofile "admin"
set vdom "root"
set remote-group "foobar"
set password ENC potatoes
next

1 Answers1

0

Your syntax is messed up. I'm guessing you want

#!/bin/bash

echo "config system admin" >output.txt

sed -n '/bash/s/:.*//p' /etc/passwd |
while read -r uname; do
  if [ "$uname" = "root" ]; then
    echo "skipping '$uname'" >&2
  else
    cat <<____
edit $uname
set remote-auth enable
set trusthost1 8.8.8.8 255.255.255.255
set accprofile "admin"
set vdom "root"
set remote-group "foobar"
set password ENC potatoes
next
____
  fi
done >>output.txt

You probably tried to type \n (not /n) to indicate a literal newline, but the string already contains all the newlines you need.

I refactored to avoid a useless cat and a useless grep but the basic syntactic fixes are probably more important to pay attention to if you are only just learning the basics.

Using cat with a here-document over echo is probably not a crucial change here, but simplifies the nested quoting conundrum you were getting yourself into.

If (as indicated in a comment) you would like to skip many users, not just root, perhaps switch to a case statement:

while read -r uname; do
  case $uname in
    "root" | "fred" | "barney" | "dino")
      echo "skipping '$uname'" >&2;;
  *)
    cat <<____;;
...
____
  esac
done 

This syntax looks befuddling at first but it's not dangerous, just different to what most other languages look like. The * case is like the else in the earlier script and every branch needs to be terminated with a double semicolon.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • One more question. For, if [ "$uname" = "root" ]; then echo "skipping $uname 'root'" >&2 If I wanted to add multiple users here besides "root" how would I make the echo "skipping $uname 'root'" >&2 "root" bit a variable, then add multiple users in here ? if [ "$uname" = "root" ]; then – HatterTheMadd Aug 06 '20 at 18:57
  • It's probably easier to switch to a `case` statement which easily lets you list a bunch of alternatives. I'll update the answer with some more code. – tripleee Aug 06 '20 at 19:01
  • Ah I figured it out. It's: if [ "$uname" = "root" ] || [ "$uname" = "test" ] || [ "$uname" = "user" ]; then echo "skipping $uname" >&2 – HatterTheMadd Aug 06 '20 at 19:14
  • But I'd still like to see the "case" option as well. Thanks for the help! – HatterTheMadd Aug 06 '20 at 19:15