简体   繁体   中英

Error when running a unix 'at' job

The code is:

at -k $wval <<ENDMARKER

while [[ -s Usr_List ]]; do
    for i in $(cat Usr_List); do
        if finger -m | grep $i; then
            echo "$i is online" | elm $mval
            sed "/$i/d" <Usr_List >tmplist
            mv tmplist Usr_List
        fi
     done
done

ENDMARKER

Looking at the actual at job, it is

while [[ -s Usr_List ]]; do
    for i in jim
joe 
tim; do
        if finger -m |grep ; then
            echo " is online" | elm jack
            sed "//d" <Usr_List >tmplist
            mv tmplist Usr_List
        fi
     done
done

jim joe and tim are the names on the list Usr_List

It seems like the cat Usr_List and the $i are the problem but I am not sure how to fix this.

Thanks

EDIT: The at job sends an email saying that jim was unexpected.

Check man at , in particular the part about ".... executed at a later time, using /bin/sh.". The script you are showing is valid bash , but depending on what /bin/sh is on your system, may not be valid syntax.

What's Happening

The problem is that the lines in your here-doc are being expanded when you input the at command. That means that the shell expands $(cat Usr_List) into:

jim
joe
tim

And it expands $i into the empty string, because it is not yet defined.

What You Want to Happen

You really want those variables to be expanded when the at command fires, not when you're telling the at command what to do.

How to fix it

In order to keep the shell from expanding variables in a here-doc, you can simply quote your delimiter at the beginning. So, change your first line:

at -k $wval <<ENDMARKER

to

at -k $wval <<"ENDMARKER"

and it will work great.

Incidentally,

I'm curious, as my at command doesn't have a -k option--what is that supposed to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM