LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   msg bash script (http://www.linuxquestions.org/questions/programming-9/msg-bash-script-438566/)

unkn0wn 04-25-2006 03:54 AM

msg bash script
 
How can i build shell script for example:

-script name mesg
-when user type mesg prompt is displayed
-> type your message to $user
> msg
> msg

-when $user log in he read mesg which is sent by some user.

Artanicus 04-25-2006 05:42 AM

a quick example, adapt and extend, don't use it just plain as I wrote it..
Code:

#!/bin/bash
if [ -z $1 ]; then
echo "Usage: $0 <target_user>"
exit 1
fi

echo "Enter your msg:"
echo -n "-> "
read msg

echo "$USER wrote you on $(date): $msg >> /tmp/$1-mesg
echo "=======================" >> /tmp/$1-mesg
# has to have write permissions via some way, not that safe d:

And /etc/profile could have something like this:
Code:

cat /tmp/$USER-mesg && echo > /tmp/$USER-mesg
It needs alot more error-checking and stuff like tyhat but something to work from (:

unkn0wn 04-28-2006 03:21 AM

tnx thats it.
any sugestions to delete first three line in file.

Artanicus 04-28-2006 04:31 AM

Well.. I'd prolly do that in C++ myself.. Alot more effective than toying around with wc and tail.. But it would look something like this:
Code:

#!/bin/bash
lines=$(wc -l $1 | cut -d " " -f 1)
tail -n $(echo lines - 3 | bc) $1

Not pretty, but it works (;

muha 04-28-2006 05:01 AM

Cut the three first lines of <file> with sed (looks better :D):
Code:

sed -i '1,3d' file

Artanicus 04-28-2006 05:13 AM

Quote:

Originally Posted by muha
Cut the three first lines of <file> with sed (looks better :D):
Code:

sed -i '1,3d' file

wow.. looks like its time to start learning sed.. It looks pretty powerful (:

muha 04-28-2006 05:30 AM

It is very, yes. Try the sed one-liners if you want to get started: http://bookmarks.linuxquestions.org/...tries/tags/sed


All times are GMT -5. The time now is 02:08 PM.