{"id":3648,"date":"2017-10-28T13:36:03","date_gmt":"2017-10-28T05:36:03","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3648"},"modified":"2017-10-28T13:36:03","modified_gmt":"2017-10-28T05:36:03","slug":"linux-shell%e5%ad%a6%e4%b9%a0-%e4%b9%8b-etcinit-dfunctions-%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3648.html","title":{"rendered":"Linux Shell\u5b66\u4e60 \u4e4b \/etc\/init.d\/functions \u8be6\u89e3"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u7b80\u5355\u5b66\u4e60\u4e00\u4e0bLinux\u4e0b\u539f\u751f\u7684functions\u811a\u672c\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4e3a\u81ea\u5df1\u7f16\u5199Bash\u811a\u672c\u63d0\u4f9b\u4e00\u4e9b\u53c2\u8003\u3002<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<pre class=\"lang:default decode:true \">[root@sec-test ~]# ls -lt \/etc\/init.d\/functions\r\n-rw-r--r-- 1 root root 17500 May  3 06:17 \/etc\/init.d\/functions\r\n\r\n[root@sec-test ~]# rpm -qf \/etc\/init.d\/functions\r\ninitscripts-9.49.39-1.el7.x86_64<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true\"># \u5b57\u7b26\u4e32\u5305\u542b\u5224\u65ad\r\n# returns OK if $1 contains $2\r\nstrstr() {\r\n  [ \"${1#*$2*}\" = \"$1\" ] &amp;&amp; return 1\r\n  return 0\r\n}\r\n\r\n# \u5224\u65ad\u7ed9\u51fa\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u8868\u793a\u7684\u6587\u4ef6\u540d\u662f\u5426\u4e3a\u7279\u5b9a\u540e\u7f00\r\n# Check whether file $1 is a backup or rpm-generated file and should be ignored\r\nis_ignored_file() {\r\n    case \"$1\" in\r\n\t*~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave)\r\n\t    return 0\r\n\t    ;;\r\n    esac\r\n    return 1\r\n}\r\n\r\n# \u5224\u65ad\u63d0\u4f9b\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5426\u4e3a\u300ct\/T\/y\/Y\/yes\/YES\/Yes\/...\/true\/...\u300d\r\n# Evaluate shvar-style booleans\r\nis_true() {\r\n    case \"$1\" in\r\n\t[tT] | [yY] | [yY][eE][sS] | [tT][rR][uU][eE])\r\n\treturn 0\r\n\t;;\r\n    esac\r\n    return 1\r\n}\r\n\r\n# \u5224\u65ad\u63d0\u4f9b\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5426\u4e3a\u300cf\/F\/n\/N\/no\/NO\/No\/...\/false\/...\u300d\r\n# Evaluate shvar-style booleans\r\nis_false() {\r\n    case \"$1\" in\r\n\t[fF] | [nN] | [nN][oO] | [fF][aA][lL][sS][eE])\r\n\treturn 0\r\n\t;;\r\n    esac\r\n    return 1\r\n}\r\n\r\n# \u5224\u65ad\u53c2\u6570\u4e2d\u7ed9\u5b9a\u7684pid\u5bf9\u5e94\u8fdb\u7a0b\u662f\u5426\u5b58\u5728\r\n# Check if any of $pid (could be plural) are running\r\ncheckpid() {\r\n\tlocal i\r\n\r\n\tfor i in $* ; do\r\n\t\t[ -d \"\/proc\/$i\" ] &amp;&amp; return 0\r\n\tdone\r\n\treturn 1\r\n}\r\n\r\n#\u8d77\u5230\u7c7b\u4f3creadlink\u547d\u4ee4\u7684\u4f5c\u7528\r\n__readlink() {\r\n    ls -bl \"$@\" 2&gt;\/dev\/null| awk '{ print $NF }'\r\n}\r\n\r\n# \u8f93\u51fa\u6307\u5b9a\u8fdb\u7a0b\u7684pid\u5217\u8868(\u901a\u8fc7\u8c03\u7528pidof\u547d\u4ee4\u5b9e\u73b0)\r\n# Output PIDs of matching processes, found using pidof\r\n__pids_pidof() {\r\n\tpidof -c -m -o $$ -o $PPID -o %PPID -x \"$1\" || \\\r\n\t\tpidof -c -m -o $$ -o $PPID -o %PPID -x \"${1##*\/}\"\r\n}\r\n\r\n# \u4ee5\u5b88\u62a4\u8fdb\u7a0b\u7684\u65b9\u5f0f\u542f\u52a8\u4e00\u4e2a\u8fdb\u7a0b\r\n# A function to start a program.\r\ndaemon() {\r\n}\r\n\r\n# \u6740\u6389\u8fdb\u7a0b\u7684\u51fd\u6570\r\n# A function to stop a program.\r\nkillproc() {\r\n}\r\n\r\n# \u591a\u5f69\u6253\u5370\r\necho_success() {\r\n}\r\necho_failure() {\r\n}\r\necho_warning() {\r\n}\r\n...\r\n\r\n# \u8bb0\u5f55\u64cd\u4f5c\u5e76\u4ee5\u591a\u5f69\u6837\u5f0f\u8f93\u51fa\u5176\u6267\u884c\u7ed3\u679c\r\n# Run some action. Log its output.\r\naction() {\r\n  local STRING rc\r\n\r\n  STRING=$1\r\n  echo -n \"$STRING \"\r\n  shift\r\n  \"$@\" &amp;&amp; success $\"$STRING\" || failure $\"$STRING\"\r\n  rc=$?\r\n  echo\r\n  return $rc\r\n}\r\n\r\n<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">#!\/bin\/bash\r\n# Date:        20171024\r\n# set -x\r\n\r\n# Source function library.\r\nif [ -f \/etc\/init.d\/functions ]; then\r\n    . \/etc\/init.d\/functions\r\nelse\r\n\techo '\"\/etc\/init.d\/functions\" not exists'\r\n\techo 'try \"rpm -ql initscripts | grep --color \/functions\"'\r\n\texit 0\r\nfi\r\n\r\nexport PATH=\/usr\/local\/bin:\/bin:\/usr\/bin:\/sbin:\/usr\/sbin\/:\/usr\/local\/sbin\r\n\r\n#action 'date +\"%F %T\"\r\n#' date +\"%F %T\"\r\n## [ \"$?\" -eq 0 ] &amp;&amp; echo_success || echo_failure\r\n#\r\n## date +\"%F %T\"\r\n#action 'rpm -ql \/etc\/init.d\/functions\r\n#' rpm -ql \/etc\/init.d\/functions\r\n#\r\n## [ \"$?\" -eq 0 ] &amp;&amp; echo_success || echo_failure\r\n#$SETCOLOR_SUCCESS &amp;&amp; echo \"success\" &amp;&amp; $SETCOLOR_NORMAL\r\n#$SETCOLOR_FAILURE &amp;&amp; echo \"failure\" &amp;&amp; $SETCOLOR_NORMAL\r\n#$SETCOLOR_WARNING &amp;&amp; echo \"warning\" &amp;&amp; $SETCOLOR_NORMAL\r\n#\r\n#echo_s() {\r\n#    $SETCOLOR_SUCCESS &amp;&amp; echo \"$@\" &amp;&amp; $SETCOLOR_NORMAL\r\n#}\r\n#\r\n#echo_f() {\r\n#    $SETCOLOR_FAILURE &amp;&amp; echo \"$@\" &amp;&amp; $SETCOLOR_NORMAL\r\n#}\r\n#\r\n#echo_w() {\r\n#    $SETCOLOR_WARNING &amp;&amp; echo \"$@\" &amp;&amp; $SETCOLOR_NORMAL\r\n#}\r\n#\r\n#echo_s haha\r\n#echo_f haha\r\n#echo_w haha\r\n\r\n\r\ns=$\"Starting $prog (via systemctl): \"    #\u5728\u5b57\u7b26\u4e32\u524d\u9762\u6dfb\u52a0 $ \u662f\u4e3a\u5565\uff1f\r\necho $s\r\n\r\ns2=\"Starting $prog (via systemctl): \"    #\u6d4b\u8bd5\u6765\u770b\u6ca1\u5565\u533a\u522b\r\necho $s2\r\n\r\n#\u83b7\u53d6\u5c4f\u5e55\u5bbd\u5ea6\r\n[ -z \"${COLUMNS:-}\" ] &amp;&amp; COLUMNS=80\r\necho $COLUMNS\r\n\r\naction 'checkpid 1' checkpid 1\r\naction 'checkpid 19999' checkpid 19999\r\n\r\n# returns OK if $1 contains $2\r\naction 'strstr()' strstr \"this is the whole string.\" \"search_string\"\r\n\r\naction 'is_true yes' is_true yes\r\naction 'is_true yeS' is_true yeS\r\naction 'is_true Yes' is_true Yes\r\naction 'is_true yEs' is_true yEs\r\naction 'is_true yy' is_true yy\r\n\r\naction 'is_false no' is_false no\r\naction 'is_false No' is_false No\r\naction 'is_false N' is_false N\r\naction 'is_false false' is_false false\r\naction 'is_false x' is_false x<\/pre>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.cnblogs.com\/sunfie\/p\/5149678.html\">Linux Shell\u5b66\u4e60 \u4e4b \/etc\/init.d\/functions \u8be6\u89e3<\/a><\/li>\n<li><a href=\"http:\/\/www.yangwenjun.net\/post\/213\/\">\/etc\/init.d\/functions \u8be6\u89e3[linux \u7cfb\u7edf\u811a\u672c]<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u5b66\u4e60\u4e00\u4e0bLinux\u4e0b\u539f\u751f\u7684functions\u811a\u672c\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4e3a\u81ea\u5df1\u7f16\u5199Bash [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7,12],"tags":[83,959],"class_list":["post-3648","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","category-tools","tag-bash","tag-functions"],"views":5458,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3648"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3648\/revisions"}],"predecessor-version":[{"id":3649,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3648\/revisions\/3649"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}