Checking ebay for registered emails shell - shell

I am actually a beginner shell script developer, and i was given a script to work on, but i have not been able to successfully fix it, even after several days of studying. I want the script below to actually check a site and tell me which emails are already registered on the from the list of emails i will parse to it, and also save this emails.
The main challenge is that the code doesn't run at all, but i have a feeling i am close to making it work, and the fact that i always get the error "/bin/bash^M: bad interpreter: No such file or directory" when i try to run the code just sucks. I need help. Thanks
Below is the code i have at the moment.
#!bin/bash
#ebay.sh
CheckBay(){
wget -q "https://reg.bay.com/reg/ajax?
email=${em}&countryId=1&mode=5&eId=email" -O tmp/ganteng.txt
cat tmp/ganteng.txt | grep "Your email address is already registered
with eBay" > /dev/null;cekh=$?
if [ $cekh -eq 0 ];then
echo -e "[+] $em = \033[1m\e[1;32m[Valid in ebay]\E[0m"
echo -e "[+] $em = \033[1m\e[1;32m[Valid in ebay]\E[0m" >> log.txt
echo "$em" >> validebay.txt
else
echo -e "[+] $em = \033[1m\e[1;31m[Not Valid in ebay]\E[0m"
echo -e "[+] $em = \033[1m\e[1;31m[Not Valid in ebay]\E[0m" >> log.txt
echo "$em" >> notvalidebay.txt
fi
rm -f tmp/*
}
run(){
for em in `cat $email`
do
CheckBay $em
done
}
detail(){
if [ ! -d tmp ];then
mkdir tmp
fi
if [ ! -f $list ];then
echo "[?] file $email Not Found [!]"
exit
fi
}
read -p "[+] Enter list email = " email
detail
run
N/B: This is learning please, and i really need to be better

Related

How to get a meaningful output from grep?

I am writing a bash script that you can run on a server where mediawikis of games are deployed. I want to check if the wikis use SMTP. So a part of my script greps for the content in the config file that proves that they use SMTP.
The problem is, that I do that on multiple servers and I want to loop through multiple markets on a server. Not all servers share the same market names. In my script I have arrays that contain ALL market names. I want my script to account for the case that the grep cant find the file in which it is to look up if SMTP is used. How can I go about that?
I was thinking about a extra command to ask if the file exists before grepping. But that didn't work out as you can
for i in "${markets[#]}"; do
myPATH="/www/${game}_$i/${game}_$i/LocalSettings.php"
grepOut="grep -q 'wgSMTP = array(' "$myPATH""
if grep -q "wgSMTP = array(" "$myPATH"; then
echo -e "The Market ${BLUE}$i${NC} ${GREEN}uses${NC} SMTP."
else
if [[ "$grepOut" == *"No such file or directory"* ]]; then
if [[ "$market" == "all" ]]; then
echo -e "All markets:"
else
echo -e "The Market ${BLUE}$i doesn't${NC} exist on this server."
fi
else
echo -e "The Market ${BLUE}$i${NC} ${RED}doesn't${NC} use SMTP."
fi
fi
done
for i in "${markets[#]}"; do
path="/www/${game}_$i/${game}_$i/LocalSettings.php"
if ! [[ -f "$path" ]]; then
if [[ "$market" == "all" ]]; then
echo -e "All markets:"
else
echo -e "The Market ${BLUE}$i doesn't${NC} exist on this server."
fi
continue
fi
if grep -q 'wgSMTP = array(' "$path"; then
echo -e "The Market ${BLUE}$i${NC} ${GREEN}uses${NC} SMTP."
else
echo -e "The Market ${BLUE}$i${NC} ${RED}doesn't${NC} use SMTP."
fi
done
Not sure if $market should be $i (or vice versa).

using cronic to reduce email notifications in a wrapper script for successful backups

I've been using cronic to silence emails from cron jobs when the job is successful. I'm trying to customize it so when a response code is 0 and the error output matches a string of "mount: /VessRAID/RH: /dev/sde1 already mounted on /VessRAID/RH.", to not send an email. Below is the script, then the contents of the email then my attempt at trying to suppress the email which is not working. Any idea what I may be doing wrong?
#!/bin/bash
# Cronic v3 - cron job report wrapper
# Copyright 2007-2016 Chuck Houpt. No rights reserved, whatsoever.
# Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/
set -eu
TMP=$(mktemp -d)
OUT=$TMP/cronic.out
ERR=$TMP/cronic.err
TRACE=$TMP/cronic.trace
set +e
"$#" >$OUT 2>$TRACE
RESULT=$?
set -e
PATTERN="^${PS4:0:1}\\+${PS4:1}"
if grep -aq "$PATTERN" $TRACE
then
! grep -av "$PATTERN" $TRACE > $ERR
else
ERR=$TRACE
fi
if [ $RESULT -ne 0 -o -s "$ERR" ]
then
echo "Cronic detected failure or error output for the command:"
echo "$#"
echo
echo "RESULT CODE: $RESULT"
echo
echo "ERROR OUTPUT:"
cat "$ERR"
echo
echo "STANDARD OUTPUT:"
cat "$OUT"
if [ $TRACE != $ERR ]
then
echo
echo "TRACE-ERROR OUTPUT:"
cat "$TRACE"
fi
fi
rm -rf "$TMP"
Here is what the email notification looks like:
Cronic detected failure or error output for the command:
/usr/local/sbin/reg-backup-cronic.sh daily
RESULT CODE: 0
ERROR OUTPUT:
mount: /VessRAID/RH: /dev/sde1 already mounted on /VessRAID/RH.
STANDARD OUTPUT:
/dev/sde1 on /VessRAID/RH type ext4 (rw,relatime)
Here is my attempt at a wrapper script:
#!/bin/bash
/usr/local/sbin/reg-backup.sh $1
CODE=$?
err=$TRACE
if [[ $CODE -eq 0 && $err = "mount: /VessRAID/RH: /dev/sde1 already mounted on /VessRAID/RH." ]]
then
exit $CODE
fi
Alas the emails continue.
Hat tip to the creator of cronic, Chuck Houpt, for cluing me in to an answer, which was to look at the original script and why the error is happening. Case-sensitivity got the best of me:
if mount | grep Vessraid; then
echo starting $1 backup >> /var/log/vessraid.log
Notice the case in VessRAID should have been:
if mount | grep VessRAID; then
echo starting $1 backup >> /var/log/vessraid.log
Now emails only happen when there really is an error.

Bash sub script redirects input to /dev/null mistakenly

I'm working on a script to automate the creation of a .gitconfig file.
This is my main script that calls a function which in turn execute another file.
dotfile.sh
COMMAND_NAME=$1
shift
ARG_NAME=$#
set +a
fail() {
echo "";
printf "\r[${RED}FAIL${RESET}] $1\n";
echo "";
exit 1;
}
set -a
sub_setup() {
info "This may overwrite existing files in your computer. Are you sure? (y/n)";
read -p "" -n 1;
echo "";
if [[ $REPLY =~ ^[Yy]$ ]]; then
for ARG in $ARG_NAME; do
local SCRIPT="~/dotfiles/setup/${ARG}.sh";
[ -f "$SCRIPT" ] && echo "Applying '$ARG'" && . "$SCRIPT" || fail "Unable to find script '$ARG'";
done;
fi;
}
case $COMMAND_NAME in
"" | "-h" | "--help")
sub_help;
;;
*)
CMD=${COMMAND_NAME/*-/}
sub_${CMD} $ARG_NAME 2> /dev/null;
if [ $? = 127 ]; then
fail "'$CMD' is not a known command or has errors.";
fi;
;;
esac;
git.sh
git_config() {
if [ ! -f "~/dotfiles/git/gitconfig_template" ]; then
fail "No gitconfig_template file found in ~/dotfiles/git/";
elif [ -f "~/dotfiles/.gitconfig" ]; then
fail ".gitconfig already exists. Delete the file and retry.";
else
echo "Setting up .gitconfig";
GIT_CREDENTIAL="cache"
[ "$(uname -s)" == "Darwin" ] && GIT_CREDENTIAL="osxkeychain";
user " - What is your GitHub author name?";
read -e GIT_AUTHORNAME;
user " - What is your GitHub author email?";
read -e GIT_AUTHOREMAIL;
user " - What is your GitHub username?";
read -e GIT_USERNAME;
if sed -e "s/AUTHORNAME/$GIT_AUTHORNAME/g" \
-e "s/AUTHOREMAIL/$GIT_AUTHOREMAIL/g" \
-e "s/USERNAME/$GIT_USERNAME/g" \
-e "s/GIT_CREDENTIAL_HELPER/$GIT_CREDENTIAL/g" \
"~/dotfiles/git/gitconfig_template" > "~/dotfiles/.gitconfig"; then
success ".gitconfig has been setup";
else
fail ".gitconfig has not been setup";
fi;
fi;
}
git_config
In the console
$ ./dotfile.sh --setup git
[ ?? ] This may overwrite existing files in your computer. Are you sure? (y/n)
y
Applying 'git'
Setting up .gitconfig
[ .. ] - What is your GitHub author name?
Then I cannot see what I'm typing...
At the bottom of dotfile.sh, I redirect any error that occurs during my function call to /dev/null. But I should normally see what I'm typing. If I remove 2> /dev/null from this line sub_${CMD} $ARG_NAME 2> /dev/null;, it works!! But I don't understand why.
I need this line to prevent my script to echo an error in case my command doesn't exists. I only want my own message.
e.g.
$ ./dotfile --blahblah
./dotfiles: line 153: sub_blahblah: command not found
[FAIL] 'blahblah' is not a known command or has errors
I really don't understand why the input in my sub script is redirected to /dev/null as I mentioned only stderr to be redirected to /dev/null.
Thanks
Do you need the -e option in your read statements?
I did a quick test in an interactive shell. The following command does not echo characters :
read -e TEST 2>/dev/null
The following does echo the characters
read TEST 2>/dev/null

Bash script exits unexpectedly

I am writing a wrapper script around mail. There is a function in the program that I need for looping back to the main menu, but just before the function is declared, the program just exits back to the main prompt. Here is the code:
function restart ()
{
m
}
clear
echo Working...
echo If you are prompted for your sudo password or asked if you want to continue, then you are being
echo prompted to install mailutils. This is normal upon first-time use, or
echo use on a computer without mailutils installed.
echo
echo Starting in 5 seconds...
sleep 5
echo Examining dependencies...
dpkg -l | grep -qw mailutils || sudo apt-get install mailutils
echo Starting client...
function m ()
{
clear
echo Welcome to the Terminal GMail Client, or TGC!
echo Please enter your gmail address:
read acc
name=${acc%#*}
echo Welcome, $name! Would you like to read[R] or write[W] emails?
read opt
if [ $opt=="R" ] || [ $opt=="r" ]
then
echo Working...
sleep 1
clear
mail -u $acc -p
restart
elif [ $opt=="W" ] || [ $opt=="w" ]
then
clear
echo Working...
sleep 1
clear
echo Enter the subject here:
read sub
echo Enter the recipients address here:
read rec
echo Enter carbon copy [CC] here or leave blank for none:
read cc
echo Enter blind carbon copy [Bcc] here or leave blank for none:
read bcc
echo Enter the body of the email here:
read body
echo Sending to $rec...
mail -s $sub -c $cc -b $bcc --user=$acc $rec "$body"
echo Done! Going to main menu in 2 seconds...
sleep 2
restart
fi
}
You see, there is no error, and I am put back at the prompt right after line 15, after 'Starting Client...'.
As others have pointed out in the comments: there's no need for multiple shell functions and recursion - a simple while loop will do.
The following is a revised version of your code with proper quoting and rudimentary error handling. Your script will need a lot more input validation and error checking to stand the test of real-world use.
But perhaps this will get you started.
#!/usr/bin/env bash
clear
echo 'Working...'
echo 'If you are prompted for your sudo password or asked if you want to continue, then you are being
prompted to install mailutils. This is normal upon first-time use, or
use on a computer without mailutils installed.'
echo 'Starting in 5 seconds...'
sleep 5
echo 'Examining dependencies...'
dpkg -l | grep -qw mailutils || sudo apt-get install mailutils || exit
clear
echo 'Starting client...'
while true; do
echo 'Welcome to the Terminal GMail Client, or TGC!'
echo 'Please enter your gmail address:'
read -r acc
name=${acc%#*}
echo "Welcome, $name! Would you like to read[R] or write[W] emails or quit[Q]?"
read -r opt
case $opt in
r|R)
echo 'Working...'
sleep 1
clear
mail -u "$acc" -p || { echo "ERROR: Please try again." >&2; continue; }
;;
w|W)
clear
echo 'Working...'
sleep 1
clear
echo 'Enter the subject here:'
read -r sub
echo "Enter the recipient's address here:"
read -r rec
echo 'Enter carbon copy [CC] here or leave blank for none:'
read -r cc
echo 'Enter blind carbon copy [Bcc] here or leave blank for none:'
read bcc
echo 'Enter the body of the email here:'
read -r body
echo "Sending to $rec..."
mail -s "$sub" -c "$cc" -b "$bcc" --user="$acc" "$rec" "$body" || { echo "ERROR: Please try again." >&2; continue; }
echo 'Done! Going to main menu in 2 seconds...'
sleep 2
;;
q|Q)
echo 'Goodbye.'
exit 0
;;
*)
echo 'ERROR: Unknown command. Please try again.' >&2
;;
esac
done

if loop within for loop in shell script not working

I have the following shell script in place to send out an email alert. When I only have the for loop everything works fine but when I try to add the if condition it does not seem to work and returns a null message body everytime. A snippet from the script is as below:
for host in $servers
do
connections=`ssh $host "netstat -a | grep ES | wc -l"`
echo "$connections" >> debug.log
echo "$host" >> debug.log
if [$connections -gt 0]
then
echo "------------------------------------------------------------------" >> $emailmessage
echo "Host: $host needs to be checked" >> $emailmessage
echo "------------------------------------------------------------------" >> $emailmessage
echo "$connections" >> $emailmessage
else
echo "Everything is fine"
fi
done
I always end up getting the line 21: [49: command not found error when i try to execute the script. I tried debugging but I figured out my script is not even getting inside the if loop.
Can someone please let me know what am I missing?
Thanks in advance.
/rd
There should be space around [ and ] also its better to put variable in quotes, so update your line as
...
if [ "$connections" -gt 0 ] ;
then
...

Resources