No Syntax Error and No Output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting No Syntax Error and No Output
# 1  
Old 05-17-2010
No Syntax Error and No Output

Hello All,

this script should use the command who, to log the Full Name, Date, and IP Address of the users who are connected to the system in a file called logfile. Excluding the user running the script.

the output in the log file is also sorted, id like to think my scripting is logical, but unfortunately i'm not getting any output in the logfile. and im not getting any syntax errors either.


Code:
        
        user=$(whoami | cut -d" " -f1 )
        who > temp
        lines=$(wc -l ./temp | cut -d" " -f1)
        cnt=1
        
        touch tmplog    
        touch logfile

        while [ $cnt -le $lines ]
        do
            usertemp=$(head -$cnt ./temp | tail -1 | cut -d" " -f1)
            nametemp=$(grep $usertemp /etc/passwd | cut -d: -f5)
            datetemp=$(head -$cnt ./temp | tail -1 | cut -d" " -f13)
            adrstemp=$(head -$cnt ./temp | tail -1 | cut -d" " -f14)

            if [ "$usertemp" != "$user" ]
            then 
                echo -n $nametemp >> tmplog
                echo -n " " >> tmplog
                echo -n $datetemp >> tmplog
                echo -n " " >> tmplog
                echo -n $adrstemp >> tmplog
                echo "" >> tmplog
            fi
            timetemp=$(head -$cnt ./temp | tail -1 | cut -d" " -f1)
            let cnt=$cnt+1             
        done        
        
        sort -db tmplog -o logfile
        rm temp
        rm tmplog

# 2  
Old 05-17-2010
Hi.

To begin, this won't work:
Code:
lines=$(wc -l ./temp | cut -d" " -f1)

The reason is that -d takes every occurrence to be a field separator.

wc -l returns multiple leading spaces (separators).

A quick change would be:
Code:
lines=$(wc -l ./temp | awk '{print $1}')

edit: silly me, see below Smilie

Last edited by Scott; 05-17-2010 at 03:54 PM..
This User Gave Thanks to Scott For This Post:
# 3  
Old 05-17-2010
or,
Code:
lines=$(wc -l < ./temp)

This User Gave Thanks to clx For This Post:
# 4  
Old 05-17-2010
A better way to loop through a file could be:
Code:
while read line
do
    usertemp=$(echo "$line" | cut -d" " -f1)
    nametemp=$(grep $usertemp /etc/passwd | cut -d: -f5)
    datetemp=$(echo "$line" | cut -d" " -f13)
    adrstemp=$(echo "$line" | cut -d" " -f14)

    if [ "$usertemp" != "$user" ]
    then 
        echo -n $nametemp >> tmplog
        echo -n " " >> tmplog
        echo -n $datetemp >> tmplog
        echo -n " " >> tmplog
        echo -n $adrstemp >> tmplog
        echo "" >> tmplog
    fi
done < ./temp

There should be better ways to do the job, can you post some lines of the file ./temp and the desired output?
This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 05-17-2010
@scottn: i have a couple of questions,

what is this command, awk '{print $1}' is it compatible with Fedora?

what is the output for this?!
Code:
lines=$(wc -l < ./temp)

i used this command to save the number of lines, so i can use them in a loop while reading the file, a programming approach i guess, since im new to scripting and im not sure how to read the lines in a file
Code:
lines=$(wc -l ./temp | cut -d" " -f1)

@Franklin52
the ./temp file is only a temporary file holding the information from the who command, after that i read each line separately, and use it to save the full name, time and IP which i save in tmplog, that i sort and the final output is in logfile

the output should be something like this
Jack_Oneal 12:23 172.168.12.1
# 6  
Old 05-17-2010
Quote:
Originally Posted by ibzee33
@Franklin52
the ./temp file is only a temporary file holding the information from the who command
Ok, can you post the output of some lines?
# 7  
Old 05-17-2010
awk is an utility/language to process text data.its available on all modern unix based systems ( including fedora).

in your case, basically it gives you the first column of whatever you are getting from wc -l command.

Code:
/home/->wc -l log
14 log
/home/->wc -l log | awk '{print $1}'
14

same thing can be done with the indirection:
Code:
/home/->wc -l < log
14
/home/->


Next, Regarding reading of the file, yes,you don't need to count the lines and loop over it.

there is a built-in for this purpose. i.e read
Code:
while read line
do
 echo $line # do whatever with each line
done < ./temp

This User Gave Thanks to clx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

IF section problem. syntax error: unexpected end of file error

Hello, I have another problem with my script. Please accept my apologies, but I am really nooby in sh scripts. I am writing it for first time. My script: returned=`tail -50 SapLogs.log | grep -i "Error"` echo $returned if ; then echo "There is no errors in the logs" fi And after... (10 Replies)
Discussion started by: jedzio
10 Replies

2. UNIX for Dummies Questions & Answers

Dot and redirected output syntax in a tar command

Hi All, Please could anyone advise what the purpose of the dot syntax in the following command means: tar -cvf ${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE} . >/${BACKUP_ROOT}/${ARCHIVE_LOG} Many thanks (2 Replies)
Discussion started by: daveu7
2 Replies

3. Shell Programming and Scripting

awk syntax mistake doubles desired output

I am trying to add a line to a BASH shell script to print out a large variable length table on a web page. I am very new to this obviously, but I tried this with awk and it prints out every line twice. What I am doing wrong? echo "1^2^3%4^5^6%7^8^9%" | awk 'BEGIN { RS="%"; FS="^"; } {for (i =... (6 Replies)
Discussion started by: awknewb123
6 Replies

4. Shell Programming and Scripting

problem with print append to output file syntax

I'm trying to output the contents of the infile to the outfile using Append. I will want to use append but the syntax doesn't seem to be working ! Input file (called a.txt) contains this: a a a b b b I'm running shell script (called k.sh) from Unix command-line like this: ./k.sh .... (1 Reply)
Discussion started by: script_op2a
1 Replies

5. OS X (Apple)

Syntax for output filenames

Hello to everybody... I have spent very long time on this before I decided to sign up in this forum. Maybe I've just not used the right keywords. I have to say that I'm not a programmer or something like that... I've been playin' around a little with ImageMagick and I would like to have... (4 Replies)
Discussion started by: pixelanstalt
4 Replies

6. Shell Programming and Scripting

Script for a syntax output for a table.

Hello Unix Gurus, I need a few instuructions how to approach the following scenarios.For a given TABLENAME a script should display the following structure on the screen.The structure should be as below.for a given perticular table. SELECT *FROM TAB1 a JOIN TAB1 b ON ... (0 Replies)
Discussion started by: kanakaraju
0 Replies

7. Shell Programming and Scripting

Receiving error: ./ang.ksh[35]: 0403-057 Syntax error at line 116 : `done' is not expected.

Hi All I am quite new to Unix. Following is a shell script that i have written and getting the subject mentioned error. #!/bin/ksh #------------------------------------------------------------------------- # File: ang_stdnld.ksh # # Desc: UNIX shell script to extract Store information.... (3 Replies)
Discussion started by: amitsinha
3 Replies

8. AIX

nim mksysb error :/usr/bin/savevg[33]: 1016,07: syntax error

-------------------------------------------------------------------------------- Hello, help me please. I am trying to create a mksysb bakup using nim. I am geting this error, how to correct it ? : Command : failed stdout: yes stderr: no... (9 Replies)
Discussion started by: astjen
9 Replies

9. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies
Login or Register to Ask a Question