Solved

linux - shell script

Posted on 2014-01-26
12
379 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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.

740 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