You are not logged in.

#1 2014-11-15 03:15:21

quasifilmie
Member
Registered: 2011-10-27
Posts: 296

[SOLVED] comparing strings in if else statement

Hey guys I have been silent on this board for a long time. Still using Arch.

I have a script:

#2014-11-14

#read certain users with ksh shell
grep -c ^[a-d,m-p].*ksh$ /etc/passwd && read users

echo -e "There are $users users starting with a-d and m-p on the server \nwith a login shell of ksh"

#get largest file and if more than 5kb zip to scrap directory
		
LFS="$(find ~ -type f -printf "%k %p\n" | sort -rn | head -1 | cut -d ' ' -f1)"

LFN="$(find ~ -type f -printf "%k %p\n" | sort -rn | head -1 | cut -d ' ' -f2)"

if [ "$LFS" -gt 5 ]; then
	mkdir -p scrap 
	zip scrap/zip.zip 
	echo Greater than 5
else 
	echo "Less than 5"
fi

#take name of user and output if they hard at work with date and time or save 

echo -e "\nPlease enter a username:\t" && read user

li1='(00:00)'
li2='logged in'
td=li="$(last | grep $user | tr -s " " | head -1 | cut -d ' ' -f6-8)"

li="$(last | grep $user | tr -s " " | head -1 | cut -d ' ' -f10)"

if [ "$li" == "$li1" ] || [ "$li" == "$li2" ]; then 
	echo "User $user is hard at work as of $td"
else 
	last | grep $user | tr -s " " | tee tardiness.txt
fi 

Everything works except the last if else statement. I am not sure if the syntax is wrong or I am not comparing the strings properly. Perhaps my strings aren't even declared properly. Basically the $li variable gets the output from the row of last column from the "last" command which should be the value of $li1 or $li2.

Any help would be much appreciated.

Last edited by quasifilmie (2014-11-15 17:47:24)

Offline

#2 2014-11-15 03:31:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: [SOLVED] comparing strings in if else statement

What results do you expect, and what results are you getting?  The syntax looks fine - but I have no idea what you are trying to do.  You could echo the strings in question right before the conditional to see if they hold what you actually think they do.

You really should simplify all those long pipelines - check out awk [1]

Also, that `last ...` pipeline that you set "li" to outputs nothing for me - there is no 10th field.


[1] The following are functionally equivalent:

last | grep $user | tr -s " " | head -1 | cut -d ' ' -f6

last | awk '/'$user'/ { print $6; exit; }'

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2014-11-15 04:08:05

quasifilmie
Member
Registered: 2011-10-27
Posts: 296

Re: [SOLVED] comparing strings in if else statement

UPDATE: Everything works. I just had to account for the different fields in the first row of last. Sometimes there are ten field. Sometimes there are not. There wasn't anything wrong with the syntax either. Thanks Trilby.

Trilby wrote:

Also, that `last ...` pipeline that you set "li" to outputs nothing for me - there is no 10th field

There is a 10th field for me that says when the first row says "stilled logged in".There is ten fields when it says that as opposed to when it says [00:00].

I now know where one of my errors is. The variable $li can never match $li2 (logged in) because I need to specify fields 9-10.

I'll post an update tomorrow on my progress. Thanks a lot for the help and especially for pointing out the awk equivalent smile

Last edited by quasifilmie (2014-11-15 17:47:08)

Offline

Board footer

Powered by FluxBB