0

I want to list all services from /etc/init.d/ and grep pattern and store them in array. for instance like below.

ls -la /etc/init.d/ | grep example-subs | awk '{ print $9 }'
example-subs-sidekick
example-subs-unicorn

and store the strings in array those are returned. but somehow it is not happening. I used below commands.

 gawk -F: '{ print $9 }' ls -la /etc/init.d/ | grep example-subs
 gawk -F: '{ print $9 }' "ls -la /etc/init.d/ | grep example-subs"
 gawk -F: '{ print $9 }' 'ls -la /etc/init.d/ | grep example-subs'
 gawk -F: '{ print $9 }' `ls -la /etc/init.d/ | grep example-subs`
 awk '{ print $9 ls -la /etc/init.d/ | grep example-subs }'
 awk '{ print $9 "ls -la /etc/init.d/ | grep example-subs" }'
 awk '{ print $9 `ls -la /etc/init.d/ | grep example-subs` }'
 awk '{ print $9 (ls -la /etc/init.d/ | grep example-subs) }'
 awk '{ print $9 "(ls -la /etc/init.d/ | grep example-subs)" }'

But nothing works. Can someone help here.

Shailesh Sutar
  • 1,517
  • 5
  • 23
  • 41

1 Answers1

0

Use your first command as part of the assignment to the array:

array=($(ls -a /etc/init.d/ | grep example-subs))

You don't need to use awk. If you use ls instead of ls -l, the only thing it prints is the filename.

Barmar
  • 364
  • 1
  • 8