Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

linux - shell script

Posted on 2014-01-26
12
378 Views
Last Modified: 2014-03-15
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
Comment
Question by:ittechlab
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 39810239
chkconfig  --list | grep '5:on' | cut -f1

Open in new window

0
 

Author Comment

by:ittechlab
ID: 39810260
how can i read one by one using while loop.


chkconfig  --list | grep '5:on' | cut -f1
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39810722
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 8

Assisted Solution

by:Surrano
Surrano earned 250 total points
ID: 39811316
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
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39811345
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
 
LVL 8

Expert Comment

by:Surrano
ID: 39811364
Yeah I'm pretty sure command line limit won't kick in here. This is why I wrote "theoretically"
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39830312
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
 

Author Comment

by:ittechlab
ID: 39830371
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
 
LVL 19

Accepted Solution

by:
simon3270 earned 250 total points
ID: 39830407
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
 
LVL 27

Expert Comment

by:skullnobrains
ID: 39835524
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
 

Author Comment

by:ittechlab
ID: 39868487
thanks everyone for the answers.
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39870129
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question