linux shell脚本 wget,bash – 在shell脚本中运行wget和其他命令

以下是编写shell脚本时要使用的一些准则.

>使用它们时始终引用变量.这有助于避免误解的可能性. (如果文件名包含空格怎么办?)

>不要相信像rm这样的命令上的文件加载.请改用循环. (如果文件名以连字符开头怎么办?)

>尽可能避免使用子壳.带反引号的线条让我发痒.

>如果你能提供帮助,请不要执行.特别是在你的exec实际运行之后,不要指望脚本的任何部分.

我应该指出,虽然你的shell可能是bash,但你已经指定了/ bin / sh来执行这个脚本,所以它不是一个bash脚本.

这是一个重写错误检查.加盐调味.

#!/bin/sh

# Linux

wget=/usr/bin/wget

tar=/bin/tar

apachectl=/usr/sbin/apache2ctl

# FreeBSD

#wget=/usr/local/bin/wget

#tar=/usr/bin/tar

#apachectl=/usr/local/sbin/apachectl

TXT="GOT TO THE END, YEAH"

WORKING_DIR="/var/asl/updates"

TARGET_DIR="/usr/local/apache/conf/modsec_rules/"

EXISTING_FILES_DIR="/var/asl/updates/modsec/"

EXISTING_ARCH="/var/asl/updates/"

URL_BASE="http://updates.atomicorp.com/channels/rules/subscription"

WGET_OPTS='--user="jim" --password="xxx-yyy-zzz"'

if [ ! -x "$wget" ]; then

echo "ERROR: No wget." >&2

exit 1

elif [ ! -x "$apachectl" ]; then

echo "ERROR: No apachectl." >&2

exit 1

elif [ ! -x "$tar" ]; then

echo "ERROR: Not in Kansas anymore, Toto." >&2

exit 1

fi

# change to working directory and cleanup any downloaded files

# and extracted rules in modsec/ directory

if ! cd "$WORKING_DIR"; then

echo "ERROR: can't access working directory ($WORKING_DIR)" >&2

exit 1

fi

# Delete each file in a loop.

for file in "$EXISTING_FILES_DIR"/* "$EXISTING_ARCH_DIR"/modsec-*; do

rm -f "$file"

done

# Move old VERSION out of the way.

mv VERSION VERSION-$$

# wget1 to download VERSION file (replaces WGET1)

if ! $wget $WGET_OPTS $URL_BASE}/VERSION; then

echo "ERROR: can't get VERSION" >&2

mv VERSION-$$VERSION

exit 1

fi

# get current MODSEC_VERSION from VERSION file and save as variable,

# but DON'T blindly trust and run scripts from an external source.

if grep -q '^MODSEC_VERSION=' VERSION; then

TARGET_DATE="`sed -ne '/^MODSEC_VERSION=/{s/^[^=]*=//p;q;}' VERSION`"

echo "Target date: $TARGET_DATE"

fi

# Download current archive (replaces WGET2)

if ! $wget ${WGET_OPTS} "${URL_BASE}/modsec-$TARGET_DATE.tar.gz"; then

echo "ERROR: can't get archive" >&2

mv VERSION-$$VERSION # Do this, don't do this, I don't know your needs.

exit 1

fi

# extract archive

if [ ! -f "$WORKING_DIR/modsec-${TARGET_DATE}.tar.gz" ]; then

echo "ERROR: I'm confused, where's my archive?" >&2

mv VERSION-$$VERSION # Do this, don't do this, I don't know your needs.

exit 1

fi

tar zxvf "$WORKING_DIR/modsec-${TARGET_DATE}.tar.gz"

for file in "$EXISTING_FILES_DIR"/*; do

cp "$file" "$TARGET_DIR/"

done

# So far so good, so let's restart apache.

if $apachectl configtest; then

if $apachectl restart; then

# Success!

rm -f VERSION-$$

echo "$TXT"

else

echo "ERROR: PANIC! Apache didn't restart. Notify the authorities!" >&2

exit 3

fi

else

echo "ERROR: Apache configs are broken. We're still running, but you'd better fix this ASAP." >&2

exit 2

fi

请注意,虽然我已经将其重写为更加明智,但仍然有很大的改进空间.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值