1 (03.03.2012 10:39:54 отредактировано mil612)

Собственно имеется скрипт syslog-ng, в нем есть команды:

echo "# Start the next generation system logger." >> /etc/rc.d/rc.M  
echo "if [ -x /etc/rc.d/rc.syslog-ng ]; then" >> /etc/rc.d/rc.M  
echo "  . /etc/rc.d/rc.syslog-ng start " >> /etc/rc.d/rc.M  
echo "fi" >> /etc/rc.d/rc.M

которые добавляются в скрипт rc.M.
Как сделать так, чтобы в случае неоднократного выполнении скрипта syslog-ng, он не добавлял лишний раз вышеописанные команды, если в скрипте rc.M уже имеется строка 'next generation system logger' ?
Сам скрипт приведен ниже

+ открыть спойлер

#! /bin/sh

# Install some packages
installpkg ./eventlog-0.2.7-i486-1gufo.tgz
installpkg ./syslog-ng-3.0.2-i486-1gufo.tgz
# Getting IP on eth0
IP=`ifconfig | grep 'inet ' | cut -f 2 -d ":" | cut -f 1 -d " " |grep '' -m 1`;
# Generating /etc/syslog-ng/syslog-ng.conf
cat << EOF > /etc/syslog-ng/syslog-ng.conf
@version:3.0
options { long_hostnames(off); flush_lines(0); };
source src { unix-stream("/dev/log"); internal(); };
source from_all { udp (ip($IP) port(514)); };

destination to_all { file ( "/var/log/HOSTS/\$HOST/\$HOST.log" perm(0666) dir_perm(0755)  create_dirs(yes) ); };
destination authlog { file("/var/log/auth.log"); };
destination cron { file("/var/log/cron.log"); };
destination daemon { file("/var/log/daemon.log"); };
destination kern { file("/var/log/kern.log"); };
destination lpr { file("/var/log/lpr.log"); };
destination user { file("/var/log/user.log"); };
destination uucp { file("/var/log/uucp.log"); };
destination ppp { file("/var/log/ppp.log"); };
destination iptables { file("/var/log/iptables.log"); };

destination debug { file("/var/log/debug"); };
destination messages { file("/var/log/messages"); };
destination console { usertty("root"); };
destination console_all { file("/dev/tty12"); };
#destination loghost { udp("loghost" port(999)); };

destination xconsole { pipe("/dev/xconsole"); };

filter f_auth { facility(auth); };
filter f_authpriv { facility(auth, authpriv); };
filter f_syslog { not facility(authpriv, mail); };
filter f_cron { facility(cron); };
filter f_daemon { facility(daemon); };
filter f_kern { facility(kern); };
filter f_lpr { facility(lpr); };
filter f_user { facility(user); };
filter f_uucp { facility(cron); };
filter f_ppp { facility(local2); };
filter f_debug { not facility(auth, authpriv, news, mail); };
filter f_messages { level(info..warn) and not facility(auth, authpriv, mail, news); };
filter f_emergency { level(emerg); };

filter f_info { level(info); };
filter f_notice { level(notice); };
filter f_warn { level(warn); };
filter f_crit { level(crit); };
filter f_err { level(err); };
filter iptables { level(debug); };

log { source(from_all); destination(to_all); };
log { source(src); filter(f_authpriv); destination(authlog); };
log { source(src); filter(f_cron); destination(cron); };
log { source(src); filter(f_daemon); destination(daemon); };
log { source(src); filter(f_kern); destination(kern); };
log { source(src); filter(f_lpr); destination(lpr); };
log { source(src); filter(f_user); destination(user); };
log { source(src); filter(f_uucp); destination(uucp); };
log { source(src); filter(f_debug); destination(debug); };
log { source(src); filter(f_messages); destination(messages); };
log { source(src); filter(f_emergency); destination(console); };
log { source(src); filter(f_ppp); destination(ppp); };
log { source(src); filter(iptables); destination(iptables); };
log { source(src); destination(console_all); };
EOF

cp ./rc.syslog-ng /etc/rc.d/rc.syslog-ng
chmod 755 /etc/rc.d/rc.syslog-ng
chmod 644 /etc/rc.d/rc.syslog
#cp ./rc.M /etc/rc.d/rc.M
#if  cat /etc/rc.d/rc.M | grep 'next generation system logger'; then
    echo "# Start the next generation system logger." >> /etc/rc.d/rc.M
    echo "if [ -x /etc/rc.d/rc.syslog-ng -a -x /usr/sbin/syslog-ng -a -d /var/log ]; then" >> /etc/rc.d/rc.M
    echo "  . /etc/rc.d/rc.syslog-ng start " >> /etc/rc.d/rc.M
    echo "fi" >> /etc/rc.d/rc.M
#fi
killall syslogd
killall syslog-ng
syslog-ng
#tar -zxvf ./Python-3.1.3.tgz
#cd ./Python-3.1.3
#./configure

2

как-то так:

if [ -z "$(grep 'next generation system logger' /etc/rc.d/rc.M)" ]; then
    echo "# Start the next generation system logger." >> /etc/rc.d/rc.M  
    echo "if [ -x /etc/rc.d/rc.syslog-ng ]; then" >> /etc/rc.d/rc.M  
    echo "  . /etc/rc.d/rc.syslog-ng start " >> /etc/rc.d/rc.M  
    echo "fi" >> /etc/rc.d/rc.M
fi
95% процентов проблем находятся между клавиатурой и стулом.

3 (03.03.2012 11:10:37 отредактировано bormant)

Ответ уже был дан ранее в теме: http://linuxforum.ru/viewtopic.php?pid=255255#p255255
Собственно конкретные ответы:
http://linuxforum.ru/viewtopic.php?pid=255257#p255257
http://linuxforum.ru/viewtopic.php?pid=255337#p255337

Что-то вроде

if  [ ! grep 'next generation system logger' /etc/rc.d/rc.M ] ; then
    cat << EOF >> /etc/rc.d/rc.M
    # Start the next generation system logger.
    if [ -x /etc/rc.d/rc.syslog-ng -a -x /usr/sbin/syslog-ng ] ; then
        . /etc/rc.d/rc.syslog-ng start
    fi
EOF
fi

4

По поводу этого ответа http://linuxforum.ru/viewtopic.php?pid=255337#p255337 я уже писал дальше, что возникает ошибка при выполнении скрипта, пробелы ставил.
в вашем скрипте чего-то не хватает или что-то лишнее

if  [ ! grep 'next generation system logger' /etc/rc.d/rc.M ] ; then
    cat << EOF >> /etc/rc.d/rc.M
    # Start the next generation system logger.
    if [ -x /etc/rc.d/rc.syslog-ng -a -x /usr/sbin/syslog-ng ] ; then
        . /etc/rc.d/rc.syslog-ng start
    fi
EOF
fi

ругается на несуществующую строку

 Консоль:
./install_syslog.sh: line 92: syntax error: unexpected end of file

а всего строк 91.

5 (03.03.2012 12:16:54 отредактировано bormant)

if  grep 'next generation system logger' /etc/rc.d/rc.M > /dev/null; then
    echo "rc.M patched already."
else
    echo "Patching rc.M..."
    cat << EOF >> /etc/rc.d/rc.M
    # Start the next generation system logger.
    if [ -x /etc/rc.d/rc.syslog-ng -a -x /usr/sbin/syslog-ng ]; then
        . /etc/rc.d/rc.syslog-ng start
    fi
EOF
fi

Или ещё короче

grep 'next generation system logger' /etc/rc.d/rc.M > /dev/null || cat << EOF >> /etc/rc.d/rc.M
    # Start the next generation system logger.
    if [ -x /etc/rc.d/rc.syslog-ng -a -x /usr/sbin/syslog-ng ] ; then
        . /etc/rc.d/rc.syslog-ng start
    fi
EOF

Или как подсказали выше

if [ -z "$(grep 'next generation system logger' /etc/rc.d/rc.M)" ]; then
    cat << EOF >> /etc/rc.d/rc.M
    # Start the next generation system logger.
    if [ -x /etc/rc.d/rc.syslog-ng -a -x /usr/sbin/syslog-ng ]; then
        . /etc/rc.d/rc.syslog-ng start
    fi
EOF
fi

В первых 2-х случаях для ветвления используется код возврата grep, в 3-м проверяется текст, выведенный grep, а точнее его отсутствие (-z).

6

Или как в ранее упомянутой теме по отрицанию кода возврата grep:

if  ! grep 'next generation system logger' /etc/rc.d/rc.M > /dev/null; then
    echo "Patching rc.M..."
    cat << EOF >> /etc/rc.d/rc.M
    # Start the next generation system logger.
    if [ -x /etc/rc.d/rc.syslog-ng -a -x /usr/sbin/syslog-ng ]; then
        . /etc/rc.d/rc.syslog-ng start
    fi
EOF
fi

7

Всем спасибо! Все варианты работают.
Задача решена.