linux 程序如何解析shell中的#

朱小厮
博客专家认证
2015-10-15 05:28:23
按照常理来说 shell中除了第一行的#/bin/bash之外的#都是注释,但是我看到某些脚本的#后面的值是有用的,不是单存的注释而已,比如下面keepalived的脚本片段:
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived

这里的#后面的几个参数是程序用到的。
那么请问我怎么写可以读取这个#后面的参数值呢?
...全文
230 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
mymtom 2015-10-16
man chkconfig RUNLEVEL FILES Each service which should be manageable by chkconfig needs two or more commented lines added to its init.d script. The first line tells chkconfig what runlevels the service should be started in by default, as well as the start and stop priority levels. If the service should not, by default, be started in any runlevels, a - should be used in place of the runlevels list. The second line contains a description for the service, and may be extended across multiple lines with backslash continuation. For example, random.init has these three lines: # chkconfig: 2345 20 80 # description: Saves and restores system entropy pool for \ # higher quality random number generation. This says that the random script should be started in levels 2, 3, 4, and 5, that its start priority should be 20, and that its stop priority should be 80. You should be able to figure out what the description says; the \ causes the line to be continued. The extra space in front of the line is ignored. chkconfig also supports LSB-style init stanzas, and will apply them in preference to "chkconfig:" lines where available. A LSB stanza looks like: ### BEGIN INIT INFO # Provides: foo # Required-Start: bar # Defalt-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Description: Foo init script ### END INIT INFO In this case, the start priority of "foo" would be changed such that it is higher than the "bar" start prior- ity, if "bar" is enabled. Care must be taken when adding dependencies, as they can cause vast shifts in the start and stop priorities of many scripts.
  • 打赏
  • 举报
回复
朱小厮 2015-10-16
下面是我写的tomcat脚本,缺了#chkconfig这个注释,就不能正确添加到service中 #!/bin/bash #chkconfig: 2345 10 90 #description: Starts and Stops the tomcat daemon #edited by ZZH: 2015-10-14 CATALINA_HOME=/users/shr/apache-tomcat-7.0.64 JAVA_HOME=/users/shr/util/JavaDir/jdk export CATALINA_HOME export JAVA_HOME start_tomcat=$CATALINA_HOME/bin/startup.sh stop_tomcat=$CATALINA_HOME/bin/shutdown.sh if [ ! -f $CATALINA_HOME/bin/catalina.sh ] then echo "===Tomcat is not available.===" exit fi start() { echo -n "===Starting tomcat===:" ${start_tomcat} echo "===tomcat start ok.===" } stop() { echo -n "===Shutting down tomcat===:" ${stop_tomcat} echo "===tomcat stop ok.===" } status() { ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrap start" | grep -v "grep" | awk '{printf $1 " "}' | wc | awk '{print $2}' > tomcat_process_count.txt read line < tomcat_process_count.txt rm tomcat_process_count.txt if [ $line -gt 0 ] then echo -n "tomcat ( pid = " ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrap start" | grep -v "grep" | awk '{printf $1 " "}' | awk '{print $1}' > tomcat_process_pid.txt read pid < tomcat_process_pid.txt rm tomcat_process_pid.txt echo -n $pid echo -n ") is running..." echo else echo "tomcat is stopped" fi } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 1 start ;; status) status ;; *) echo "Usage:$0 {start|stop|restart}" exit 1 esac exit 0 那么这个怎么解释
  • 打赏
  • 举报
回复
朱小厮 2015-10-16
还是想问如果要写程序 怎么识别shell中的#?
  • 打赏
  • 举报
回复
朱小厮 2015-10-16
  • 打赏
  • 举报
回复
vachel66 2015-10-15
这几行明显是脚本的注释,是告诉你有这几个参数的,至于怎么用和读取这几个参数,你把脚本看明白吧
  • 打赏
  • 举报
回复
发帖
Linux/Unix社区

2.2w+

社区成员

Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
帖子事件
创建了帖子
2015-10-15 05:28
社区公告
暂无公告