Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

linux - shell script

Here is my requirement.

I need to read all the services at run level 5 and display to the user.

for example it should display the result as follows.

The following service are currently running on your system and turned on.

1) NetworkManager
2) abrtd          
3) acpid          
4) atd          
5) auditd          

The following services are currently not running on your system and turned off.

1) ntpd            
2) ntpdate        
3) postfix        
4) psacct        
5) rdisc          


 a) would you like to stop any service?
   if yes then user should be able to type the number next to the service and it should do the following.
1) service service_name start
2) chkconfig service_name off

  b) would you like to start any service?
  if yes then user should be able to type the number next to the service and it should do the following.
1) service service_name start
2) chkconfig service_name on



======================

when i am using while loop can I do like this to read the service? what is wrong here.

while anyunixcomamnd
do
statement
done

while x=chkconfig --list
do
echo $x
done
0
ittechlab
Asked:
ittechlab
  • 3
  • 3
  • 2
  • +3
2 Solutions
 
farzanjCommented:
chkconfig  --list | grep '5:on' | cut -f1

Open in new window

0
 
ittechlabLinux SupportAuthor Commented:
how can i read one by one using while loop.


chkconfig  --list | grep '5:on' | cut -f1
0
 
Duncan RoeSoftware DeveloperCommented:
for i in $(chkconfig  --list | grep '5:on' | cut -f1); do echo $i; done

Open in new window

Once you see what $i looks like, you can decide how you wish to process it
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SurranoSystem EngineerCommented:
Practlcally, the for-cycle with embedded execution $(...) is sufficient for you.
Theoretically, if there'd be too many services (I believe output more than 4096 characters) then you'd have to use xargs or read. I prefer the latter:

chkconfig --list | grep '5:on' | cut -f1 | while read svc; do
  echo $svc
done

Open in new window

0
 
Duncan RoeSoftware DeveloperCommented:
I think it's a lot more than 4096 characters. GNU utilities (like bash) try very hard not to impose  arbitrary limits. Personally, I have never hit a limit when using bash.
0
 
SurranoSystem EngineerCommented:
Yeah I'm pretty sure command line limit won't kick in here. This is why I wrote "theoretically"
0
 
simon3270Commented:
The limit is, as you say, now much larger - my RedHat system shows a maximum of 2615636 bytes.  It's not just bash that imposes the limit - the kernel may only allow a certain amount of space for the command, its arguments, and any environment variables set when the command is called.

If you run "xargs --show-limits" on a GNU system, it should show your limits.

I used to get hit very often by quite small command line limits on older systems, so even now I tend to use the "xargs" or "while read" methods more than the "for ii in $(...)" one.
0
 
ittechlabLinux SupportAuthor Commented:
Your environment variables take up 1830 bytes
POSIX upper limit on argument length (this system): 2617562
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2615732
Size of command buffer we are actually using: 131072
0
 
simon3270Commented:
So your kernel will allow 2,615,732 bytes (with your current environment), but xargs is only using 131,072 bytes (still quite large, but I have seen attempted command lines longer than this - the usual one is a very long set of paths in the output of a "find" command).

I still think you are pretty safe using "for ii in $(...)" for chkconfig output.  If you are at all worried, try
    chkconfig --list | wc -c
to see what the maximum size might be.
0
 
skullnobrainsCommented:
bump on @Surrano
even if the limit is high, there is no reason in writing code that may break and definitely eat up much more memory then they need to

you may wish to have a look at dialog for the UI part
0
 
ittechlabLinux SupportAuthor Commented:
thanks everyone for the answers.
0
 
SurranoSystem EngineerCommented:
Maybe you meant to close the question but forgot to accept a solution? (or more solutions, for that matter)
Please do so if you feel that the question has been answered.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now