how to start c program from bash script under apache - c

Can please anyone help me.
i have the following script:
#!/bin/bash
cache_query=$QUERY_STRING
xxx=`echo $cache_query | grep -o 'x=[0-9]*' | sed 's/x=//g'`;
yyy=`echo $cache_query | grep -o 'y=[0-9]*' | sed 's/y=//g'`;
zzz=`echo $cache_query | grep -o 'z=[0-9]*' | sed 's/z=//g'`;
let "zzz=18-$zzz"
content_type=`echo $cache_query | grep -o 'type_image=[a-z]*' | sed 's/type_image=//g'`;
db_name=`echo $cache_query | grep -o 'db=[a-z]*' | sed 's/db=//g'`;
echo "Content-type: image/"${content_type}
echo ""
case "$db_name" in
"genshtab" ) db_path="/home/bases/gena.sqlite";;
"yasat" ) db_path="/srv/ftp/upload/yasat.sqlitedb";;
esac
echo "x = " $xxx "y = " $yyy "z = " $zzz "type = " $content_type "database = " $db_name "db_path = " $db_path; #оставляю на всякий $
./tget ${db_path} ${db_name} "/mnt/tmpfs" ${content_type} ${zzz} ${yyy} ${xxx}
cat /mnt/tmpfs/${db_name}/${zzz}/${yyy}/${xxx}.${content_type}
i wrote tget program on c, so if i write
./tget /srv/ftp/upload/yasat.sqlitedb yasat /mnt/tmpfs png 17 0 1
in bash line everything go great, but if i try to call this program from bash script under apache
http://46.182.21.31/?type_image=png&db=yasat&x=2&y=1&z=15
the program doesn't start and no errors in my log,
is somebody have any ideas why?

i was resolve a trouble the next way start c program with sudo
sudo ./tget ......
and add to /etc/sudoers ability to user www-data to make sudo queries without password

Related

How to grep success in a for loop

Struggling with this...
for i in `cat services.txt`
do
if ! grep -q $i; then
echo " $i Is NOT Running"
else
echo " Checking $i on `hostname`..."
ps aux | grep -i $i | awk '{print $1, $11}'| cut -d' ' -f1-2| sort
echo -e " "
sleep 4
fi
done
The block just hangs - Ive not been able to capture the success/failure of grep
If a string in services.txt is NOT found ... the script hangs... Id like for grep to skip it if not found
services.txt contain just single words
thanks!
The reason your script hangs is beacuse the command grep -q $i is waiting for an input. You can try running that command separately in a shell and verify that it prompts for an input.
Changing your command to ps aux | grep -i $i in the if statement should fix your issue.
NOTE: ps aux | grep -i $i lists grep also as one of the process. Make sure you exclude that process by piping it to another grep ps aux | grep -i $i | grep -v 'grep'
here is the working code
checkservices() {
cat >$HOME/services.txt <<EOF
ganglia
hbase
hdfs
hive
hue
livy
mapred
test-missing-service
mysql
oozie
presto
spark
yarn
zeppelin
EOF
for i in `cat $HOME/services.txt`
do
if `ps -ef | grep ^$i | grep -v grep >/dev/null`
then
i=$(echo "$i" | awk '{print toupper($0)}')
echo "$i -- is running on `hostname`"
echo ""
sleep 2
else
i=$(echo "$i" | awk '{print tolower($0)}')
echo "$i -- IS NOT running on `hostname` error"
echo ""
fi
done
}

Shell Script not creating an Array List

I need help with a strange problem.
This line is not creating an array list, I don't know why.
date_final=($(echo $((date_end-date_start)) | grep -o "[0-9].*")) # PROBLEM
The Whole code is below, the particular array converts a wrong date into a new date but it should be an arraylist, and it creates a single variable instead.
#! /bin/bash
cd /var/lib/zabbixsrv/externalscripts/Manager
rm Unique.txt
declare -a date_final='()'
total_count=$(cat amazon.html 2>/dev/null | jq '.meta.total_count' | grep -o "[^\"]*")
i=0;
for i in $(seq 0 $total_count)
do
#compare=($(cat amazon.html 2>/dev/null | jq ".objects[$i].service_tag" | tr ' ' '\n'))
compare=($(cat amazon.html 2>/dev/null | jq ".objects[$i].conference" | grep -o "[^\"]*" | tr ' ' '\n'))
echo -e ${compare[#]} >> /var/lib/zabbixsrv/externalscripts/Manager/Unique.txt
done
compare1=($(cat Unique.txt | uniq -c | gawk '$1==1{print $2}'))
Number_line=$(echo ${#compare1[#]}) # PROBLEMA RESOLVIDO!!!!
let Number_line-=1
#echo -e ${compare[#]}
for i in $(seq 0 $Number_line)
do
#time=$(cat amazon.html 2>/dev/null | jq '.objects[] | select(.service_tag=='${compare1[$i]}')' | jq ".connect_time" | grep -o "[^\"]*" | grep -o "[^T][0-9].*" | grep -o "[0-9]\{2\}:[0-9]\{2\}:[0-9].")
time=$(cat amazon.html 2>/dev/null | jq '.objects[] | select(.conference=='${compare1[$i]}')' 2>/dev/null | jq ".connect_time" | grep -o "[^\"]*" | grep -o "[^T][0-9].*" | grep -o "[0-9]\{2\}:[0-9]\{2\}:[0-9].")
#echo -e ${compare1[$i]}
date_convert=$(date -d "$time 2 hour ago" +"%H:%M:%S")
date_start=$(date -d "$day $date_convert" +%s)
date_end=$(date +"%s")
date_final=($(echo $((date_end-date_start)) | grep -o "[0-9].*")) # PROBLEM
done
# rm Tenant.txt
#echo ${date_final[0]}
#echo -e ${date_final[#]}
tempo=1
i=0
echo -e $Numero_linha
echo -e ${date_final[#]}
for i in $(seq 0 $Number_line)
do
if ((${date_final[$i]} > $tempo)) 2>/dev/null; then
echo -e ${compare1[$i]}
fi
done
Thanks Very Much
I have used the code from your example to declare date_final and initialize date_start and date_end, and I actually get an array after executing your problematic statement.
I think your problem is you are repeatedly assigning a one-element array to your variable instead of adding elements.
Try this :
date_final+=( $((date_end-date_start)) )
The key here is usine the += operator, which appends instead of assigning. The rest is just cleanup, as you do not need grep to filter for digits (there are only digits in the result of the calculation), and the echo is not required either.

store the values from command into an array bash

svn mergeinfo --show-revs eligible http://svn.test.com/INT_1.0.0/ http://svn.test.com/DEV/ | cut -d"r" -f2 | cut -d" " -f1
6097
6099
when i put this in a script, i get only last value but not all:
#!/usr/bin/bash
src_url="http://svn.test.com/INT_1.0.0/"
target_url="http://svn.test.com/DEV/"
eligible_revs=(`svn mergeinfo --show-revs eligible $src_url $target_url | cut -d"r" -f2 | cut -d" " -f1`)
echo ${eligible_revs[#]}
output:
6099
If you are running Cygwin the line endings can mess it up
$ foo=(`printf 'bar\r\nbaz'`)
$ echo ${foo[*]}
baz

Adding a string to the front of array loop?

#!/bin/bash
#OLDIFS=$IFS
IFS=$'\r'
fortune_lines=($(fortune | fold -w 90))
#Screen_Session=$"{mainscreen}"
Screen_Session=`screen -ls|grep "\."|grep "("|awk '{print $1}'`
Screen_OneLiner=$(screen -p 0 -S ${Screen_Session} -X stuff "`printf "${fortune_lines[#]}\r"`")
#IFS=$OLDIFS;
for var in "${Screen_OneLiner[#]}"
do
echo "${var}"
done
ok this script works (sorta). I need to at the string "say " to front of the entire array index. Currently I can only get it to print out "say " to the first line.
We need not complicate things with contraptions like arrays - we can let good old sed do the job.
#!/bin/bash
fortune_lines=$(fortune | fold -w 90 | sed 's/^/say /')
Screen_Session=`screen -ls|grep "\."|grep "("|awk '{print $1}'`
screen -p 0 -S $Screen_Session -X stuff "$fortune_lines"

Bash: Script for finding files by mime-type

First, I am not experienced in scripting, so be gentle with me
Anyway, I tried making a script for finding files by mime-type ( audio, video, text...etc), and here's the poor result I came up with.
#!/bin/bash
FINDPATH="$1"
FILETYPE="$2"
locate $FINDPATH* | while read FILEPROCESS
do
if file -bi "$FILEPROCESS" | grep -q "$FILETYPE"
then
echo $FILEPROCESS
fi
done
It works, but as you could guess, the performance is not so good.
So, can you guys help me make it better ? and also, I don't want to rely on files extensions.
Update:
Here's what I am using now
#!/bin/bash
FINDPATH="$1"
find "$FINDPATH" -type f | file -i -F "::" -f - | awk -v FILETYPE="$2" -F"::" '$2 ~ FILETYPE { print $1 }'
Forking (exec) is expensive. This runs the file command only once, so it is fast:
find . -print | file -if - | grep "what you want" | awk -F: '{print $1}'
or
locate what.want | file -if -
check man file
-i #print mime types
-f - #read filenames from the stdin
#!/bin/bash
find $1 | file -if- | grep $2 | awk -F: '{print $1}'
#!/usr/bin/env bash
mimetypes=$(sed -E 's/\/.*//g; /^$/d; /^#/d' /etc/mime.types | uniq)
display_help(){
echo "Usage: ${0##*/} [mimetype]"
echo "Available mimetypes:"
echo "$mimetypes"
exit 2
}
[[ $# -lt 1 ]] && display_help
ext=$(sed -E "/^${1}/!d; s/^[^ \t]+[ \t]*//g; /^$/d; s/ /\n/g" /etc/mime.types | sed -Ez 's/\n$//; s/\n/\\|/g; s/(.*)/\.*\\.\\(\1\\)\n/')
find "$PWD" -type f -regex "$ext"

Resources