help using read in menu script to cat out lines in logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help using read in menu script to cat out lines in logs
# 1  
Old 09-23-2009
help using read in menu script to cat out lines in logs

What is wrong with my menu script? Do I need to continue with the read statements? All I want to do with option 4 is to cat some /var/log/files and awk out a few lines? How do I do that please?

Code:
$ cat menu.sh                                                
#!/bin/bash                                                  
#set -vx                                                     

while :
do     
  clear
  # Display menu
  echo          
  echo  "*************************************************************"
  echo  "Please choose from the following options; type the"           
  echo  "option number and hit the <Enter> key."                       
  echo                                                                 
  echo  "   1) To list names of the log files in the current DIR"          
  echo  "   2) Display today's date and time"                          
  echo  "   3) Display a sorted list of people currently logged on"    
  echo  "   4) Display whether a file is a file or a DIR"              
  echo  "   5) Create a backup for a file"                             
  echo  "   6) Find a user by First of Last name in /etc/passwd file"  
  echo  "   7) Find the manual pages for a specific command"           
  echo  "   8) Exit"                                                   
  echo                                                                 
  echo  "*************************************************************"
  read option                                                          
  case "$option" in                                                    
      1)    echo "The files in the current DIR are: "                  
            ls -al                                                      
            echo "Hit <Enter> to continue."                            
            read ignore                                                
            ;;                                                         
      2)    echo "The current date is: "                               
            date                                                       
            unset date                                                 
            echo "Hit <Enter> to continue."                            
            read ignore                                                
            ;;    

At this point, I'd like to change the menu to do this:

echo ---------------------------------
cd /var/log
# /var/log/secure section
cat /var/log/secure |grep -i 'password check failed'|awk '{print $1,$2,$3,$6,$7,
$8,$11}'|sort -u|more
cat /var/log/secure |grep -i 'password check failed'|wc -l |awk '{print $0}'
cat /var/log/secure |grep -i 'sudo'|awk '{print $1,$2,$3,$7,$8,$9,$10}'|sort -u
cat /var/log/secure |grep -i 'sudo'|wc -l |echo sudo used

cat /var/log/secure |grep -i 'su'|awk '{print $1,$2,$3,$5,$7,$8,$11}'|sort -u
cat /var/log/secure |grep -i 'authentication failure'|awk '{print $1,$2,$3,$13}'
|sort -u
cat /var/log/secure |grep -i 'rhost'|sort -u

cat /var/log/secure |grep -i 'could not identify password' |awk '{print $1,$2,$3
,$13}'|sort -u

  esac
done

I want it to cat the files and grep for certain items? Do I need another read statement? What should it be? I'd like to get this done today, as my meeting is in 4 hours. Any help would be appreciated. I tried this and it didn't work:

      4)    echo "Display problems with /var/log/secure and messages"
            # read fdname                                                       
  
            # if [ ! -e $fdname ]; then                                         
  
              # echo "$fdname does not exist."                                  
  
            # elif [ -d $fdname ]; then                                         
  
            #  echo "$fdname is a directory."                                   
 
            # elif [ -f $fdname ]; then                                         
  
             #  echo "$fdname is a regular file."                               
  
            # else                                                              
  
            #   echo "$fdname is something else."                               
  
            # fi                                                                
  
           # echo "Hit <Enter> to continue."                                    
 
           #  read ignore                                                       
  
            # ;;
echo ---------------------------------
cd /var/log
# /var/log/secure section
cat /var/log/secure |grep -i 'password check failed'|awk '{print $1,$2,$3,$6,$7,
$8,$11}'|sort -u|more
cat /var/log/secure |grep -i 'password check failed'|wc -l |awk '{print $0}'
cat /var/log/secure |grep -i 'sudo'|awk '{print $1,$2,$3,$7,$8,$9,$10}'|sort -u
cat /var/log/secure |grep -i 'sudo'|wc -l |echo sudo used

cat /var/log/secure |grep -i 'su'|awk '{print $1,$2,$3,$5,$7,$8,$11}'|sort -u
cat /var/log/secure |grep -i 'authentication failure'|awk '{print $1,$2,$3,$13}'
|sort -u
cat /var/log/secure |grep -i 'rhost'|sort -u

cat /var/log/secure |grep -i 'could not identify password' |awk '{print $1,$2,$3
,$13}'|sort -u

  esac
done
8)    echo "Have a nice day"
            sleep 1.5
            break
            ;;
$

Thanks.

---------- Post updated at 09:23 AM ---------- Previous update was at 09:05 AM ----------

This is what is happening now.

Code:
*************************************************************
Please choose from the following options; type the
option number and hit the <Enter> key.

   1) To list names of the log files in the current DIR
   2) Display today's date and time
   3) Display a sorted list of people currently logged on
   4) List the log files in /current_release/logs
   5) Create a backup for a file
   6) Find a user by First of Last name in /etc/passwd file
   7) Find the manual pages for a specific command
   8) Exit

*************************************************************
4
Issues with /var/log/secure
0
Hit <Enter> to continue.

when I choose option 4, it echos but won't cat out the line.

The actual code is below.

Code:
  read option                                                          
  case "$option" in                                                    
      1)    echo "The files in the current DIR are: "                  
            ls -al                                                      
            echo "Hit <Enter> to continue."                            
            read ignore                                                
            ;;                                                         
      2)    echo "The current date is: "                               
            date                                                       
            unset date                                                 
            echo "Hit <Enter> to continue."                            
            read ignore                                                
            ;;                                                         
      3)    echo "The following is a list of the users"    
            echo "currently logged in:"                                
            who | cut -d " " -f1 | sort -u                             
            echo "Hit <Enter> to continue."                            
            read ignore                                                
            ;;                                                         
      4)    echo "Issues with /var/log/secure"
            cat /var/log/secure |grep -i 'password check failed'|wc -l |awk '{pr
int $0}'
            echo "Hit <Enter> to continue."                            
             read ignore
            ;;
  esac
done
8)    echo "Thank You for Snas"
            sleep 1.5
            break
            ;;
$
$


Last edited by Franklin52; 09-23-2009 at 11:03 AM.. Reason: Please use code tags!!
# 2  
Old 09-23-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 09-23-2009
Quote:
Originally Posted by taekwondo
when I choose option 4, it echos but won't cat out the line
Actually, why should it? From my understanding,

Quote:
Originally Posted by taekwondo
Code:
cat /var/log/secure |grep -i 'password check failed'|wc -l |awk '{print $0}'

means "echo the number of lines found in file /var/log/secure containing 'password check failed'" ...!?
# 4  
Old 09-23-2009
Thanks. I did figure that out earlier and work through the issue. You are completely correct, my logic was asking for the number of issues and the number of issues was 0. Thanks for your help.
# 5  
Old 09-23-2009
Quote:
Originally Posted by taekwondo
Code:
cat /var/log/secure |grep -i 'password check failed'|wc -l |awk '{print $0}'

Did you try to win the Useless Use of Cat Award Smilie
Code:
grep -ci "password check failed" /var/log/secure

# 6  
Old 09-23-2009
No, but thanks. I am fairly new at scripting. Like maybe 2 days. Smilie

Quote:
Originally Posted by danmero
Did you try to win the Useless Use of Cat Award Smilie
Code:
grep -ci "password check failed" /var/log/secure



---------- Post updated at 02:21 PM ---------- Previous update was at 02:06 PM ----------

Another dumb question. The man pages state:

Two regular expressions may be joined by the infix operator |; the
resulting regular expression matches any string matching either subex-
pression.

I tried several times to grep for multiple items, do you have an e.g.?

[henry@impala ~]$ man grep
[henry@impala ~]$ cat junk.log|grep [hello|this]
grep: Unmatched [ or [^
bash: this]: command not found
[henry@impala ~]$ cat junk.log|grep [hello|;this]
bash: syntax error near unexpected token `;'
[henry@impala ~]$ cat junk.log|grep hello|;this
bash: syntax error near unexpected token `;'
[henry@impala ~]$ man grep
[henry@impala ~]$ cat junk.log|grep hello|this
bash: this: command not found
[henry@impala ~]$ grep -ci "hello|this" junk.log
0
[henry@impala ~]$ grep -ci hello|this junk.log
bash: this: command not found

[henry@impala ~]$ grep -ci hello;this junk.log

bash: this: command not found
# 7  
Old 09-24-2009
Quote:
Originally Posted by taekwondo
Code:
cat junk.log|grep [hello|this]

Code:
cat junk.log | grep '[hello|this]'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

Script to read last 30mins logs

Hi All, I want to read the log file for last 30mins logs with time stamps. Am using below command but, it is not working for me awk -F - -vDT="$(date --date="30 minutes ago" "+%b %_d %H:%M:%S")" ' DT < $1' log.file >tmp.txt log file time format is 2016-09-27 14:00:25,192 Use code... (1 Reply)
Discussion started by: Prashanth.K
1 Replies

3. AIX

Script to Read lines and print

Dears, I am trying to write a script to read the lines from a file in AIX Server. Below are the contents of the file. To explain, first part before = is path and second part is number. i am trying to write the script to print the path and count where count>100. any help must be appreciated... (3 Replies)
Discussion started by: sibbala
3 Replies

4. Shell Programming and Scripting

script to constantly read the last 500 new logs in a log file

Hello, I would like to write a bash script that would monitor a log file for a certain number of logs, let's say 500 logs and when it reaches that number to write the last log to another file. For example, I want to watch the /var/adm/messages and everytime, there is 500 new logs that are... (1 Reply)
Discussion started by: Pouchie1
1 Replies

5. Shell Programming and Scripting

Why does my script only read two lines of a file and not the third

I'm learning about the read command and wrote this little script to read data from a file: readfile() { while read variable; do echo $variable done } readfile < File.txt I have three lines in File.txt; each a single word. The script only echoes the first two lines and drops the... (9 Replies)
Discussion started by: Straitsfan
9 Replies

6. Shell Programming and Scripting

Menu in Menu script issue

Problem: I am trying to create a menu in a menu script and I am running into an issue with the calculator portion of the script. I am first presented with the ==Options Menu== which all 5 options working correctly. Now comes the fun part. I select option 1 which takes me to my ==Calculator... (1 Reply)
Discussion started by: iDdraig
1 Replies

7. Shell Programming and Scripting

Read files, lines into array, cat vs open

Hi Everyone, I have a file: a.txt a,b,c,d,6,6,6 1,2,3,d,6,6,6 4,5,6,6,6,6,6 #!/usr/bin/perl use warnings; use strict; my @array = (); ### Load file into array for my $i (split '\n', `cat /tmp/a.txt`) { push @array, ; } It works. But my a.txt have 1million lines, and... (2 Replies)
Discussion started by: jimmy_y
2 Replies

8. Shell Programming and Scripting

Help parsing logs maybe with menu and variables?

I would like to parse through some logs looking for things like exception or failed (grep -i failed). Ideal would be if it were in a menu format so someone without unix ability could just choose option 1 2 or 3 etc. If I could pass the hostname to a variable also that would be awesome, so someone... (5 Replies)
Discussion started by: taekwondo
5 Replies

9. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

10. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies
Login or Register to Ask a Question