shell - Bash script does not output any data in tempfile -
this code use, want collect data 4 logfile(s) in 1 file, sort , print temp filename.
#!/bin/bash tempfile=$(mktemp) user=$1 # check input #if [ -z $1 ]; echo "give me 1 username or mail address please..." # fill file user history cat /var/log/messages | grep $user >> $tempfile cat /var/log/maillog | grep $user >> $tempfile zcat /var/log/messages*gz | grep $user >> $tempfile zcat /var/log/maillog*gz | grep $user >> $tempfile # sort date cat $tempfile | sort -k1m -k2n -k3n > $tempfile echo $tempfile
cat
, zcat
everywhere useless , cannot redirect same file you're reading, 0 byte file.
try this:
#!/bin/bash tempfile=$(mktemp) user="$1" # check input #if [ -z $1 ]; echo "give me 1 username or mail address please..." # fill file user history grep "$user" /var/log/messages >> $tempfile grep "$user" /var/log/maillog >> $tempfile zgrep "$user" /var/log/messages*gz >> $tempfile zgrep "$user" /var/log/maillog*gz >> $tempfile # sort date sort -k1m -k2n -k3n "$tempfile" > "$tempfile.tmp" mv "$tempfile.tmp" "$tempfile" cat "$tempfile"
ps: don't use upper case user
variable run otherwise overwrite shell provide value variable. in general not recommended use uppercase variables in script.
Comments
Post a Comment