FreeBSDのsedコマンドで変数を使用しようとしています。 FreeBSDのsedは、aの後に\が必要です。基本的には、ファイル内の特定の行がパターンと一致する場合は、行を追加したいと思います。私はsedのそれを追加して使用しています。Bashスクリプト: aftersを期待するFreeBSDでsedを実行する際の変数の使用
#!/usr/bin/bash
SYSLOG_SERVER="192.168.1.36"
SYSLOG_PORT="514"
syslog_conf_file="/etc/syslog.conf"
send_mps_sourcetype_logs() {
logs=(messages auth.log ns.log cron license.log lastlog)
send_logs[0]=`awk '(index($2, "messages") != 0) {print $1}' $syslog_conf_file`
send_logs[1]=`awk '(index($2, "auth.log") != 0) {print $1}' $syslog_conf_file`
for ((i = 0 ; i < ${#send_logs[@]} ; i++))
do
if [ ! -z "${send_logs[$i]}" ]; then
send_logs[i]=${send_logs[i]}" \t"@$SYSLOG_SERVER:$SYSLOG_PORT
sed "/${logs[$i]}$/a\
${send_logs[$i]} \
" $syslog_conf_file
fi
done
}
send_mps_sourcetype_logs
私はこのエラーに直面しています。変数は適切に表示されますが、スクリプトを実行する方法は間違っています。これをどうすれば解決できますか? sedのため
[email protected]# bash temp.sh
send_logs *.notice;authpriv.none;kern.debug;lpr.info;mail.crit;news.err \[email protected]:514
logs messages
sed: 1: "/messages$/a ...": command a expects \ followed by text
send_logs auth.info;authpriv.info \[email protected]:514
logs auth.log
sed: 1: "/auth.log$/a ...": command a expects \ followed by text
サンプル予想入力:
[email protected]# sed '/messages$/a\
*.notice;authpriv.none;kern.debug;lpr.info;mail.crit;news.err @192.168.1.36:514 \
' /etc/syslog.conf
予想される出力:
*.err;kern.warning;auth.notice;mail.crit /dev/console
*.notice;authpriv.none;kern.debug;lpr.info;mail.crit;news.err /var/log/messages
*.notice;authpriv.none;kern.debug;lpr.info;mail.crit;news.err @192.168.1.36:514
security.* /var/log/security
auth.info;authpriv.info /var/log/auth.log
sedは実際には変数拡張を扱うように設計されていません。 –
@RamanSailopalああ、この問題を解決できないのですか? – lalit
[mcve]を私たちに提供してください。これは、 'a'コマンドと変数を使った単一のsed呼び出しであると思います。あなたの入力と希望する出力を教えてください。 –