I have a some scritps is working but not work in crontab - sh

I have a some scripts and, when ı was run manually the scripts were run. But
When working in crontab, the format is incorrect.
This is for a new Linux server
awk 'BEGIN{
FS="-"
print "<HTML>""<table border="1" border="3" cellpadding="4" cellspacing="4" bgcolor=lightblue><TH>Firma</TH><TH>Charged Party No</TH><TH>Pcom Status</TH>"
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>",$i
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
' /app/ovocontrol/cp_not_found2.txt > file.html
sed -i "s/failure/<font color="red">failure<\/font>/g;s/success/<font color="green">success<\/font>/g" file.html
(
echo "To: **********"
echo "Subject: Son 10 Dakikaya ait Toplu SMS CUDB Hata Detayi"
echo "Content-Type: text/html"
echo
cat file.html
echo
) | /usr/sbin/sendmail -t
I have a some scripts and, when ı was run manually the scripts were run. But
When working in crontab, the format is incorrect.

You don't have a shebang, nor do you have a complete crontab, so I'm guessing at what you're actually doing. I suspect you are trying to call those multiple commands directly from your crontab, which is a terrible idea. Instead, put your multiple calls into a single script and invoke it from cron. eg, do something like:
$ cat > /path/to/script << 'EOF'
#!/bin/sh
: ${f:=/app/ovocontrol/cp_not_found2.txt}
{
echo "To: **********"
echo "Subject: Son 10 Dakikaya ait Toplu SMS CUDB Hata Detayi"
echo "Content-Type: text/html"
echo
printf '<HTML><table border="1" border="3" cellpadding="4" cellspacing="4"'
printf ' bgcolor=lightblue><TH>Firma</TH><TH>Charged Party No</TH><TH>Pcom Status</TH>\n'
awk -F - ' {
printf "<TR>"
for(i=1;i<=NF;i++) printf "<TD>%s</TD>",$i
print "</TR>"
}
' "$f"
printf '</TABLE></BODY></HTML>\n'
} \
| sed -e 's#failure#<font color="red">failure</font>#g' \
-e 's#success#<font color="green">success</font>#g'
| /usr/sbin/sendmail -t
EOF
$ chmod +x /path/to/script
$ printf 'i\n0 * * * * /path/to/script\n.\nw\nq\n' | EDITOR=ed crontab -e
Note that the last command above is not really a great idea, just an attempt to codify the directive to add /path/to/script to your crontab.

I solved problem other way , "awk '!seen[$0]++'" command incorrect my format. the code was actually a short portion of the code. I think crontab has a special settings.

Related

sed - Separate quotes and arguments

So, I'm trying to get a script I'm working on to run another script in different directories with different arguments as defined in a text file.
Here's part of my code:
for bline in $(cat "$file"); do
lindir=$()
linarg=$()
echo "dir: ${lindir}"
echo "arg: ${linarg}"
done
Let's say I have a line in file that says this:
"./puppies" -c=1 -u=0 -b=1
How can I get an output of ./puppies for lindir and an output of -c=1 -u=0 -b=1 for linarg?
lindir="$( cut -d ' ' -f 1 <<<"$bline" )"
linarg="$( cut -d ' ' -f 2- <<<"$bline" )"
That is
while read -r bline; do
lindir="$( cut -d ' ' -f 1 <<<"$bline" )"
linarg="$( cut -d ' ' -f 2- <<<"$bline" )"
printf "dir: %s\n" "$lindir"
printf "arg: %s\n" "$linarg"
done <"$file"
If you're in a shell that doesn't understand "here-strings":
lindir="$( printf "%s" "$bline" | cut -d ' ' -f 1 )"
linarg="$( printf "%s" "$bline" | cut -d ' ' -f 2- )"

Replace everyting after every time different string

Want to change everything after security.server.ip=* with the result ip from the second grep.
First Grep:
cat admin.conf|grep security.server.ip|grep -v ^#
Result:
security.server.ip=10.10.1.2
Second Grep:
cat /etc/hosts|grep -i admin-server|head -1|awk '{ print $1}
Result:
10.10.1.2
Sometimes security.server.ip will be different on admin.conf and I'm wondering how to replace it with one command which will catch IP address form second grep and replace it in the first one.
You can use a script:
#!/bin/sh
IP=$(exec grep -i admin-server /etc/hosts | awk '{ print $1; exit }')
sed -i "/^security\.server\.ip=/s|=.*|=$IP|" admin.conf
You could save it in a variable:
NEWIP=`grep -i admin-server /etc/hosts|head -1|awk '{ print $1}'` \
sed -i "s/^security\.server\.ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/security\.server\.ip=$NEWIP/" admin.conf
With GNU awk for inplace editing, nextfile, and gensub():
gawk -i inplace '
NR==FNR{ if (tolower($0) ~ /admin-server/) { ip=$1; nextfile } next }
{ $0=gensub(/(security\.server\.ip=).*/,"\\1"ip,""); print }
' /etc/hosts admin.conf

How to send simple text file as attachment using HP-UX shell script?

I need to send an email with a text file as attachment using shell script in HP-UX; I don't have mutt installed.
I am using following command but it sends the file content in body of email, I want it as an attachment.
mailx -s "Report" name#example.com < file.txt
I wrote this ksh function a few years ago
# usage: email_attachment to cc subject body attachment_filename
email_attachment() {
to="$1"
cc="$2"
subject="$3"
body="$4"
filename="${5:-''}"
boundary="_====_blah_====_$(date +%Y%m%d%H%M%S)_====_"
{
print -- "To: $to"
print -- "Cc: $cc"
print -- "Subject: $subject"
print -- "Content-Type: multipart/mixed; boundary=\"$boundary\""
print -- "Mime-Version: 1.0"
print -- ""
print -- "This is a multi-part message in MIME format."
print -- ""
print -- "--$boundary"
print -- "Content-Type: text/plain; charset=ISO-8859-1"
print -- ""
print -- "$body"
print -- ""
if [[ -n "$filename" && -f "$filename" && -r "$filename" ]]; then
print -- "--$boundary"
print -- "Content-Transfer-Encoding: base64"
print -- "Content-Type: application/octet-stream; name=$filename"
print -- "Content-Disposition: attachment; filename=$filename"
print -- ""
print -- "$(perl -MMIME::Base64 -e 'undef $/; open $fh, shift; print MIME::Base64::encode(<$fh>); close $fh; ' $filename)"
print -- ""
fi
print -- "--${boundary}--"
} | /usr/lib/sendmail -oi -t
}
uuencode is your friend.
Here is a tested example:
(uuencode .vimrc vimrc.txt; uuencode .zshrc zshrc.txt; echo Here are your attachments) | mailx -s 'Mail with attachments' email_address
I was having the same problem where the output of uuencode was being sent as part of the message body rather than as an attached file (at least when using Outlook 2010 to view the sent mail). I found the answer in this thread http://www.unix.com/hp-ux/41306-sending-attachments-through-mailx.html
Adding -m causes mailx to not add MIME header lines when sending email. The OP's command would be altered to look like:
mailx -m -s "Report" name#example.com < file.txt
I also encountered the same problem few months ago.
The command I needed was ux2dos
( cat message_content_file; ux2dos file.txt | uuencode file.txt file.txt ) | mailx -m -s "subject" -r mail#sender mail#recipient
I hope it can help !
Regards

extract number from string

I have a string ABCD20110420.txt and I want to extract the date out of it. Expected 2011-04-20
I can use replace to remove the text part, but how do I insert the "-" ?
# echo "ABCD20110420.txt" | replace 'ABCD' '' | replace '.txt' ''
20110420
echo "ABCD20110420.txt" | sed -e 's/ABCD//' -e 's/.txt//' -e 's/\(....\)\(..\)\(..\)/\1-\2-\3/'
Read: sed FAQ
Just use the shell (bash)
$> file=ABCD20110420.txt
$> echo "${file//[^0-9]/}"
20110420
$> file="${file//[^0-9]/}"
$> echo $file
20110420
$> echo ${file:0:4}-${file:4:2}-${file:6:2}
2011-04-20
The above is applicable to files like your sample. If you have files like A1BCD20110420.txt, then will not work.
For that case,
$> file=A1BCD20110420.txt
$> echo ${file%.*} #get rid of .txt
A1BCD20110420
$> file=${file%.*}
$> echo "2011${file#*2011}"
20110420
Or you can use regular expression (Bash 3.2+)
$> file=ABCD20110420.txt
$> [[ $file =~ ^.*(2011)([0-9][0-9])([0-9][0-9])\.*$ ]]
$> echo ${BASH_REMATCH[1]}
2011
$> echo ${BASH_REMATCH[2]}
04
$> echo ${BASH_REMATCH[3]}
20
echo "ABCD20110420.txt" | sed -r 's/.+([0-9]{4})([0-9]{2})([0-9]{2}).+/\1-\2-\3/'
$ file=ABCD20110420.txt
$ echo "$file" | sed -e 's/^[A-Za-z]*\([0-9][0-9][0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)\.txt$/\1-\2-\3/'
This only requires a single call to sed.
echo "ABCD20110420.txt" | sed -r 's/.{4}(.{4})(.{2})(.{2}).txt/\1-\2-\3/'

How can i convert the following bash script into a perl script

#!/bin/bash
i="0"
echo ""
echo "##################"
echo "LAUNCHING REQUESTS"
echo " COUNT: $2 "
echo " DELAY: $3 "
echo " SESSID: $1"
echo "##################"
echo ""
while [ $2 -gt "$i" ]
do
i=$[$i+1]
php avtest.php $1 $4 &
echo "EXECUTING REQUEST $i"
sleep $3
done
here is a better/modified script in bash
#!/bin/bash
i="0"
#startTime=`date +%s`
startTime=$(date -u +%s)
startTime=$[$startTime+$1+5]
#startTime=$($startTime+$1+5)
dTime=`date -d #$startTime`
echo ""
echo "##################"
echo "LAUNCHING REQUESTS"
echo " COUNT: $1 "
echo " DELAY: 1 "
#echo " EXECUTION: $startTime "
echo " The scripts will fire at : $dTime "
echo "##################"
echo ""
while [ $1 -gt "$i" ]
do
i=$[$i+1]
php avtestTimed.php $1 $3 $startTime &
echo "QUEUEING REQUEST $i"
sleep 1
done
Here's a direct translation
#!/usr/bin/env perl
use strict;
use warnings;
print <<HERE;
##################
LAUNCHING REQUESTS
COUNT: $ARGV[1]
DELAY: $ARGV[2]
SESSID: $ARGV[0]
##################
HERE
my $i = 0;
while($ARGV[1] > $i){
$i += 1;
system("php avtest.php $ARGV[0] $ARGV[3] &");
print "EXECUTING REQUEST $i\n";
sleep $ARGV[2];
}
But it would make more sense to read the command line parameters into variables named after what they're for and not rely on remembering argument ordering.
A brief errata in the conversion:
I use a here string to represent multiline text. I could also have put in multiple print statements to more closely mimic the bash version
In bash arguments are accessed as numbered variables, starting with $1 and going up. In Perl the argument list is represented by the array #ARGV, which is numbered starting at zero (like arrays in most languages). In both bash and Perl the name of the script can be found in the variable $0.
In Perl arrays are written as #arrayname when refering to the entire array, but they use $arrayname[index] when accessing array members. So the Perl $list[0] is like the bash ${list[0]} and the Perl #list is like the bash ${list[#]}.
In Perl variables are declared with the my keyword; the equivalent in bash would be declare.
I've used the system function for spawning background processes. Its argument can be simply the command line as you might use it in bash.
Unlike echo, print requires to be told if there should be a newline at the end of the line. For recent versions of Perl the say function exists which will append a newline for you.
The Perl sleep function is pretty self-explanatory.
EDIT: Due to a typo $i in the print statement had been represented as $ni leading to runtime errors. This has been corrected.