Alright, so I have been asked to help with this script. Basically what it needs to do is email a user if there is an error associated with an ID. Now I won't go into too much detail on that because the sql portion does that correctly. What I don't know how to do is pull out each user email from the sql individually and email them one at a time then attach the error ticket associated with it to the email body.
My KSH script as I have it now.
#!/usr/bin/ksh
# sets all environment variables
. /data/pmes/pmes/pmes_scripts/pmes_Env_Var.config
# sets the results of the .sql file to the emailBody Variable
emailBody=`sqlplus -s $USERID/$PPASSWD#$DATABASE #/data/pmes/pmes/pmes_scripts/testingmail2.sql`
# these email addresess are emailed when the script completes
MAIL_ID='emailme#yoursite.com'
(echo "$emailBody")|mailx -s "Test Email`" $MAIL_ID
Output of echo $emailBody
aaron.heckel#yoursite.com 20140801_BR_Bob,D_PZXKGX steve.naman#yoursite.com 20140816_AM_Andrew,D_PZXKGX
(It is basically email then the issue ID. Each item has a space in between them)
Any help would be appreciated. I'm very new with mailing and sqlplus in UNIX.
Related
I am learning to write bash scripts when I ran across this doubt. Have a look at the script below. It is not the actual script but is perfect to put my point.
#!/bin/bash
firstName = "Tony"
lastName = "Stark"
init
#The command prompts here:
# > Hello!
# > bla blah
# > What is your name?
echo firstName
# > bla blah
# > Now you may enter your last name
echo lastName
Problem: So what I want to achieve is to echo only when the command prompts for What is your name?. I want to wait until prompt asks this specific question and then echo the stuff automatically. I am trying to automate this data using the script.
I tried the read command but it didn't work as thought. Is there a good way to do it? Anybody willing to shed some light is greatly appreciated. Thanks in advance!
Edit: Consider this for an instance. Please don't look at the security risks for this example. Lets automate git push.
#!/bin/bash
username: "un"
password: "pd"
git add -A
git commit -m "Update"
# returns a commit message in a second or so. It can throw an error. So `read` can not know how many lines to read. Execute the next command after reading whatso ever is the result(for now)
read message #ignore whatever the message is
git push origin master
# Now it asks for username. What I want is to echo the username on when it outputs: `Username for 'https://github.com':`
echo username
# Asks `Password for un:`
echo password
I know it has many security and functional loopholes. But I want to know is the particular way to automate echoing after a specific question is asked.
I have 2 digital clean servers - one is a few years old, one is new.
On the old server, any cron jobs that run with an echo, the echoed content is emailed to me. I'm pretty sure this happened by default - I didn't configure this myself.
On the new server, echoed content is not emailed. I have tried to send an email using the following, which worked fine, so my understanding is that email is running ok.
echo "This is the body of the email" | mail -s "This is the subject line" "me#myemail.com"
Can anyone tell me if there's a specific option for this, or if I'm missing something?
All crons email any output on stdout or stderr to the user owning the crontab.
If you don't see that mail you
look in the wrong mailbox
have something odd in $HOME/.forward
have a glitch in your crontab (odd MAILTO perhaps)
have a command that doesn't produce any output
I am using cron job to run my shell script after every 2 minutes. My shell script contains pig and hive scripts. I am searching the person with high risk using my hive query and i can get their email id from my hive table, i want to know if i can send mail to the person and how ? I checked on the internet but not able to understand properly, it would be a great help if you guys help me in this. Thanks
This code solves my problem
$ echo "hello world" | mail -s "a subject" xxx#xxx.com
I've done my research and tried lots of way but to no avail, i still could not get my postfix mail to run the script.
content of /etc/aliases
test2: "|/home/testscript.sh"
content of /home/testscript.sh Note: i've tried many kind of ways in the script. even a simple echo does not work.
#!/bin/sh
read msg
echo $MSG
i've tried running the script and it works fine.
So would you tell that it's working?
Even if you successfully direct mail to the script, you're not going to see the output of the "echo" command. If you expect to get an email response from the script, the script will need to call out to /bin/mail (or sendmail or contact an SMTP server or something) to generate the message. If you're just looking to verify that it's working, you need to create some output where you can see it -- for example, by writing the message to the filesystem:
#!/bin/sh
cat > /tmp/msg
You should also look in your mail logs (often but not necessarily /var/log/mail) to see if there are any errors (or indications of success!).
I wrote a script that sends the date and username of the person who logs in to a log file to keep a record of who has logged in. I am wondering how can you set this script to execute automatically when a user logs in rather than have to manually run it in the terminal. NOTE: the USERNAME is the current user that is logged in.
my code:
#!/bin/bash
printf "$(date) $HOSTNAME booted!\n" >> /home/USERNAME/boot.log
A more elegant way to solve this problem is to read from log files that are already being written and cannot be changed by the user. No one could say it better than Bjørne Malmanger's in his answer:
I wouldn't trust the user to GIVE you the information. As root you
TAKE it ;-)
A nice way to do this is the last command, which is great because it neatly displays all logins: Graphical, console and SSH.
last
A less elegant but still secure way is to do a grep on /var/log/auth.log. On my Gnome/Ubuntu system I can use this to track graphical logins:
grep "session opened for user USERNAME"
The right pattern for your machine needs to be found for each login type: graphical, console and SSH. This is cumbersome, but you might need to do it if you need information that goes further back than last reaches.
To directly answer your question:
You can modify the script like this to get the username
#!bin/bash
printf "$(date) $HOSTNAME booted!\n" >> /home/$(whoami)/boot.log
And add this line to /etc/profile
. /path/to/script.sh
This is not secure though because the user will be able to edit his own log
Why don't you use the last command?
I wouldn't trust the user to GIVE you the information. As root you TAKE it ;-)
Put it in ~/.bash_profile. It will be run each time they log in.
More information is available at the women's rights page (i.e. man bash).