I have a problem with a shell script. It is supposed to read arguments: index, date, time1 (the beginning of the interval), time2 (the end of the interval). It should count how many times the user(index) has logged on the given date in the time interval time1-time2.

For example : 121212 "Jan 14" 00 12

This works, but I have a problem with the argument date. It doesn't recognize it as one argument. It splits it in Jan and 14" which is a big problem. I've been searching on the internet for a few hours, but I couldn't find solution anywhere. Here is my code:


read user date time1 time2
list=`last | grep ^$user.*$date | awk '{ print $7 }'`


echo "start $start"
echo "end $end"
for el in $list ;
echo "najava $najava"

         [[ ($start = $current || $start < $current) && ($current = $end || $current < $end) ]]
if checkIf; then
        count=`expr $count + 1`
        ip=`last | grep ^$user.*$date.*$login | awk '{ print $3 }'`
        echo $ip >> address.txt
  echo "The user has logged in $count times in the given time interval"
  • Slightly off-topic, but consider reviewing my goto reference on handling command-line arguments. When referencing variables which may contain spaces, you'll want to quote the variables such that is handled as a single string and not individual arguments. – Ian W Sep 16 '15 at 10:09

Try using $1, $2, $3, $4, ... for command line arguments (instead of using read)

Call your script using:

./script.sh 121212 "Jan 14" 00 12

If you for some reason need/want read you could say:

read -r user mm dd time1 time2

date="$mm $dd"

Using -r is generally a good idea – even when one think one does not need it.

The -r option to read prevents backslash interpretation (usually used as a backslash newline pair, to continue over multiple lines). Without this option, any backslashes in the input will be discarded. You should almost always use the -r option with read.


Else, if you have control on input you could say:

121212 "Jan\ 14" 00 12

Then you do not use -r!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.