Help Needed with Creating Script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help Needed with Creating Script
# 1  
Old 10-11-2010
Help Needed with Creating Script

Hi all,

I need your help in creating a script.
At the moment, we run a daily procedure incorporating the following commands, which we would like within a script using the bash shell.

Code:
cd /var/adm/Syslog

tail -1 /var/adm/messages

grep "`date '+%b %e'`" /var/adm/messages > syslog.`date +%d%m%y`.log

ls –l

for messages in `cat syslog_message_list.txt`; do grep $messages syslog.`date +%d%m%y`.log | tr -s " " |
  cut -d" " -f4,9- | sort| uniq -c >> syslog.`date +%d%m%y`.report; done

perl -i -0pe 's/^/Count Host Error Type Message\n\n/' syslog.`date +%d%m%y`.report

cat syslog_message_list_append.txt >> syslog.`date +%d%m%y`.report

cat syslog.`date +%d%m%y`.report | mailx -r Syslog -s "Syslog Report for `date | awk '{print $1, $2, $3, $6}'`" nobody@nowhere.com

Please can you help me here so that we can run this as a scriptedron job.

Regards, Wynford

Last edited by Scott; 10-11-2010 at 08:23 AM.. Reason: Please use code tags, less formatting and a more descriptive subject
# 2  
Old 10-11-2010
Hope you know all vi editor options.
From command prompt do,
Code:
vi scriptname

then insert
Code:
#!/bin/bash

as first line followed by anything u want to do.
Save it and run it.
This User Gave Thanks to Poonamol For This Post:
# 3  
Old 10-11-2010
Power

Firstly. There is never ever a reason to use "for" when dealing with an open-ended list.
Secondly. Convoluted one-liners should be formatted scripts.

Quote:
for messages in `cat syslog_message_list.txt`; do grep $messages syslog.`date +%d%m%y`.log | tr -s " " | cut -d" " -f4,9- | sort| uniq -c >> syslog.`date +%d%m%y`.report; done

This "while" construct easier to read when properly laid out and does not include the risk of a "for" line becoming too long or not dealing with space characters correctly:

Code:
cat syslog_message_list.txt | while read messages
do
       grep "${messages}" syslog.`date +%d%m%y`.log | \
       tr -s " " | \
       cut -d" " -f4,9- | \
       sort| \
       uniq -c >> syslog.`date +%d%m%y`.report
done

Thirdly. Don't run convoluted on-liners from cron. Run a script containing appropriate environment settings - especially the Shell shebang and of course a correct $PATH which includes a path to Pearl.

Last edited by methyl; 10-11-2010 at 08:31 AM.. Reason: typo
This User Gave Thanks to methyl For This Post:
# 4  
Old 10-11-2010
Quote:
Originally Posted by methyl
Run a script containing appropriate environment settings -
This is a VERY VALUABLE suggestion which relevance can't be overestimated. The "number 1 cron problem" is like this:

i have written script x and it works fine from the command line and after i put it in cron it fails every time

and has exactly this reason: the script in question has no environment. Here is a blueprint for (cron-)scripts:

Code:
#!/bin/ksh

. /etc/environment             # for AIX
. /etc/bashrc                  # alternatively, for Linux


... here goes the rest of your code ...

exit 0

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 10-12-2010
Thanks Guys.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Creating script in rc.d

Hi, I have created customized scripts to start httpd and postgres (For CentOS 6) in /etc/init.d. However for it to work even after reboot, I have to put the script in /etc/rc.d/rc0.d, rc1.d, etc. # ls -lrt total 60 -rwxr-xr-x 1 root root 20199 Oct 4 2017 rc.sysinit -rwxr-xr-x 1... (6 Replies)
Discussion started by: anaigini45
6 Replies

2. Shell Programming and Scripting

Help Needed for creating the folder by checking today's date and, take backup using rsync command

How to create a shell script to create a folder by using the today's date to take backup using rsync command on every evening around 7 pm. Kindly help. Thanks. To be more precise, I want to create a script which matches the today's date with server's date format, if matches then creates the... (2 Replies)
Discussion started by: bakula10
2 Replies

3. Shell Programming and Scripting

Creating IN list in PLSQL script dynamically by using shell script

Hi all, I have a PLSQL script which has a IN list where it takes some ids as input. For example SELECT * FROM EMPLOYEE WHERE EMPLOYEE_ID IN (comma separated list ) I want to run this quest inside a shell script but I would like to prepare the IN list dynamically where the employee ids... (1 Reply)
Discussion started by: LoneRanger
1 Replies

4. Shell Programming and Scripting

Need help in creating file restoration script from a backup script.

Hi all i am struggling in creating a restore of env files while doing applications clone. the first file i created for copying the important configurations file which is running perfect now for reverting the changes i mean when i am restoring these files to its original places i have to do... (7 Replies)
Discussion started by: javeedkaleem
7 Replies

5. Shell Programming and Scripting

KSH - help needed for creating a script to generate xml file from text file

Dear Members, I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input. The contents of a single cell of INFO column is like: Area:app - aam Clean Up Criteria:... (0 Replies)
Discussion started by: Yoodit
0 Replies

6. Shell Programming and Scripting

Expect script help needed- script failing if router unavailable

Hey all. Sometimes I'm tasked to change some router configs for the entire network (over 3,000 Cisco routers). Most of the time its a global config parameter so its done with a loop and an IP list as its the same configuration change for all routers. This is working OK. However, sometimes an... (3 Replies)
Discussion started by: mrkz1974
3 Replies

7. HP-UX

Tru 64 bit on HP AlphaServer ES45 creating problem Help Needed !!!

Dear All, I am new on Tru 64 bit Unix platform. I have a database server "HP AlphaServer ES45" version p5.1 of Tru Ux 64 bit installed. I have 4 hard drives in this system (1,2,3,4). My 4th hard drive is damaged which contain Oracle database files including partitions on 4th drives... (2 Replies)
Discussion started by: hellozishan
2 Replies

8. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

9. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

10. Solaris

help needed in creating an image from a 7za compressed file

Hi all, I have been working on a few files that have been 7za compressed.(which are a part of an OS) Now after extracting the file and doing manipulations i did a putback. But when i create an image and burn it onto the disk to check if my changes are working... I get error at boot time. Now... (0 Replies)
Discussion started by: wrapster
0 Replies
Login or Register to Ask a Question