LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2013, 12:25 AM   #1
_mz
Member
 
Registered: Jul 2013
Posts: 33

Rep: Reputation: Disabled
command to get certain line


Hi guys.

I want to do a scripting..

# ls -l /tmp/MC* | wc -l
ls: cannot access /tmp/MC*: No such file or directory
0

How do I get the line with 0? I need it for a condition.

I tried to google around for sed or cut command but unfortunately I did not find any hint.
 
Old 09-09-2013, 12:39 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,644

Rep: Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501
A=$(ls -l /tmp/MC* | wc -l)
and you will have the value in variable A
 
Old 09-09-2013, 02:12 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
@_mz: If the error message isn't wanted/needed you can get rid of it by doing the following:
Code:
A=$(ls -l /tmp/MC* 2>/dev/null | wc -l)
The following links might help with your scripting efforts:

Bash:
 
1 members found this post helpful.
Old 09-09-2013, 02:48 AM   #4
_mz
Member
 
Registered: Jul 2013
Posts: 33

Original Poster
Rep: Reputation: Disabled
@pan64 and @druuna, thank u very much. Now I get it.

one more question. I would need to check a daemon if it is not running then script will start it up. Let say the command to check status of the daemon like:

1. # su - nails -c "sudo /etc/init.d/nails status"
nailsdctl: daemon is not running
moncg: monitor gateway is not running
the McAfeeVSEForLinux Apache server does not appear to be running

2. # ps -ef | grep nails
root 17436 9084 0 15:41 pts/1 00:00:00 grep nails

How/what should I grep and put it in condition in order to start up the daemon.
 
Old 09-09-2013, 02:58 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,644

Rep: Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501
this would be the most simple way:
ps -ef | grep nails || <start command>
but not the most secure solution
 
Old 09-09-2013, 03:06 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
I disagree with pan64.

Code:
ps -ef | grep nails || <start command>
The above command might grep the grep command. Either use:
Code:
ps -ef | grep "[n]ails" || <start command>
Or use:
Code:
ps -C nails || <start command>
 
1 members found this post helpful.
Old 09-09-2013, 03:15 AM   #7
_mz
Member
 
Registered: Jul 2013
Posts: 33

Original Poster
Rep: Reputation: Disabled
I found in internet something like this:

if [ -f /proc/$PID/exe ]; then
kill $PID
fi

This one is to kill process if pid exists. Since I want the other way around (to start process if pid does not exist), I tried to put like this:

if [ ! -f /proc/$PID/exe ]; then
<start command>
fi

but it is not working. Am I missed out something?
 
Old 09-09-2013, 03:22 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Why don't you have a look at the solutions given by pan64 and me in posts #5 and #6?

Quote:
Originally Posted by _mz View Post
I found in internet something like this:

if [ -f /proc/$PID/exe ]; then
kill $PID
fi

This one is to kill process if pid exists. Since I want the other way around (to start process if pid does not exist), I tried to put like this:

if [ ! -f /proc/$PID/exe ]; then
<start command>
fi

but it is not working. Am I missed out something?
I can only assume that you did not supply $PID.

I do believe that this is the hard way to determine if a process is or isn't running.
 
Old 09-09-2013, 03:25 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,644

Rep: Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501
@druuna, you are right, pgrep nails || <start command> or similar should be used, pgrep never returns itself.
@_mz: $PID has no value, PID=$(cat /proc/$PID/exe) probably works.
 
Old 09-09-2013, 03:34 AM   #10
_mz
Member
 
Registered: Jul 2013
Posts: 33

Original Poster
Rep: Reputation: Disabled
Code:
ps -ef | grep "[n]ails" || <start command>
This works for me. However I do not want the start command to be initiated straight away. I want to put it in if else. Let say something like this:

if
ps -ef | grep "[n]ails" = <I do not know what to put here or any better idea?>
then
<remove temporary file first>
<start command>
else
process is running
fi
 
Old 09-09-2013, 03:36 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,644

Rep: Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501
something like this:
if [ pgrep nails > /dev/null ]; then
something
else
something else
fi
 
1 members found this post helpful.
Old 09-09-2013, 03:41 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
pan64 gave one way, here's another:
Code:
ps -C nails 1>/dev/null 2>&1
if [[ "$?" == "0" ]] # check exit status
then
  echo "something"
else
  echo "something else"
fi
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cursor does not move to the bottom line and overwrite to same line on command line mesuutt Linux - Newbie 3 02-25-2012 08:04 AM
Print all PID folders from /proc line-by-line with this format (( PID: command-line )) courteous Linux - Newbie 7 12-12-2010 04:47 PM
awk command line: blank line record sep, new line field sep robertmarkbram Programming 4 02-21-2010 05:25 AM
xine command line? or some other command line only engine? lumix Linux - Software 2 08-10-2007 10:46 AM


All times are GMT -5. The time now is 12:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration