Solved

linux - shell script

Posted on 2014-01-26
12
381 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
[X]
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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

717 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