LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-06-2011, 01:13 AM   #1
ksmatthews
Member
 
Registered: Aug 2008
Posts: 86

Rep: Reputation: 15
Question challenging shell script


HI All,

I found this shell script (/usr/lib/php5/maxlifetime) which I do not fully understand ...

#!/bin/sh -e

max=1440

for ini in /etc/php5/*/php.ini; do
cur=$(sed -n -e
's/^[[:space:]]*session.gc_maxlifetime[[:space:]]*=[[:space:]]*\([0-9]\+\).*$/\1/p' $ini 2>/dev/null || true);

[ -z "$cur" ] && cur=0
[ "$cur" -gt "$max" ] && max=$cur

done

echo $(($max/60))

exit 0

Can anyone explain it to me ?

regards,

Steven M
 
Old 04-06-2011, 01:44 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Code:
#!/bin/sh -e

max=1440

# look for the php.ini file(s) in /etc/php5/*/ (all the subdirs in /etc/php5/)
for ini in /etc/php5/*/php.ini; do
# fill cur with
# 1) the content of this variable session.gc_maxlifetime
# 2) if it is not found fill cur with nothing (null) (the || true part)
cur=$(sed -n -e
's/^[[:space:]]*session.gc_maxlifetime[[:space:]]*=[[:space:]]*\([0-9]\+\).*$/\1/p' $ini 2>/dev/null || true);

# if cur is null, fill cur with zero
[ -z "$cur" ] && cur=0
# if cur is greater then max, fill max with value of cur
[ "$cur" -gt "$max" ] && max=$cur

done

# echo value of max (devided by 60 is in minutes, the value of session.gc_maxlifetime is set in seconds in php.ini)
echo $(($max/60))

# stop the script with a successful status
exit 0
About the sed part:

^[[:space:]]*session.gc_maxlifetime[[:space:]]*=[[:space:]]*\([0-9]\+\).*$

The above looks for session.gc_maxlifetime and its value [0-9]\+ and takes into consideration that there could be tabs or spaces before and after the values (the [[:space:]] parts. It also has to be on a line by itself (^ -> beginning of the libe and the closing $ -> end of the line).

The \1 in the replace part prints what is found between the \( and \) in the search part (this is called back referencing).

Hope this helps.
 
Old 04-07-2011, 12:47 AM   #3
ksmatthews
Member
 
Registered: Aug 2008
Posts: 86

Original Poster
Rep: Reputation: 15
Thumbs up

Thanks for that.

RE

# if cur is null, fill cur with zero
[ -z "$cur" ] && cur=0

Is this the same as ...

if [ -z "$cur" ] then
cur=0

regards,

Steven Matthews
 
Old 04-07-2011, 04:21 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Those are basically the same. Your second example's syntax is not correct. Fully correct would be:
Code:
if [ -z "$cur" ]
then
  cur=0
fi
Hope this helps
 
  


Reply



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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
Challenging script (for me at least!) Calling all scripters! Norwood Linux - General 8 03-07-2010 03:35 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:49 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
Open Source Consulting | Domain Registration