Solved

linux - shell script

Posted on 2014-01-26
12
375 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 26

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now