wanting to replace schema value of a sql script by providing schema value 'sdg_cdr' through shell script, but getting error - postgresql-9.1

#!/bin/bash
schema_name_new=sdg_cdr
echo "Initial truncate of tables"
sed -i 's/"$schema_name"/"$schema_name_new"/g' delete.sql
psql -f delete.sql
echo "Tables truncate completed"
echo "start time"
date
echo "end time"
date

Related

nsupdate in basic CSH script for BIND in unix

I have never used C and I'm not writing this for security reasons, i am just writing this script to test an update via nsupdate to my BIND for a specific zone being "zoneA.unix". But i am receiving "option: undefined variable" And I'm not to sure if this is the correct way to do nsupdate via a user's inputs.
echo "First of all we need to grab your username:"
set uname = $<
if ($uname == "zoneA")then
echo "password: "
set passwd = $<
if ($passwd == "Azone")then
echo "you are in"
echo "now to do the nsupdate"
echo "do you wish to (A)dd or (D)elete a record"
set numeric = $<
if ($numeric == "A")then
$option = "add"
$testinga = "add"
else if($numeric == "D")then
$option = "delete"
$testinga = "delete"
endif
echo "what to $testinga to the zone zoneA.unix?"
set innerzonename = $<
nsupdate -k /usr/local/etc/namedb/Keys/Kzonea.+157+57916.key -v
debug yes
zone zonea.unix
update $testinga $innerzonename 86400 A 127.0.0.1
show
echo "is this correct (Y)es (n)"
set sendoff = $<
if($sendoff == "Y")then
send
else if ($sendoff == "N")then
quit
endif
So the code works fine til the $option part and I'm not to sure if it will work after the input needed during the nsupdate because it won't pause it, well i don't know how i can pause it. What it seems to be doing is running nsupdate and waiting until nsupdate is finished. anyway i can pass them into it?
This Way all i am doing is pushing the nsupdate's to a text file and reading it from the text file using nsupdate -v textile. seems to be working like a charm at the moment
if ($uname == "zoneA")then
echo "password: "
set passwd = $<
if ($passwd == "Azone")then
echo "you are in"
echo "now to do the nsupdate"
echo "do you wish to add or delete a record"
set numeric = $<
echo "what to $numeric to the zone zoneA.unix?"
set innerzonename = $<
//nsupdate -k /usr/local/etc/namedb/Keys/Kzonea.+157+57916.key
echo "server "localhost"">>textfiles/stillneedanewname
echo "debug yes" >> textfiles/stillneedanewname
echo "zone zonea.unix" >> textfiles/stillneedanewname
echo "update $numeric $innerzonename.zonea.unix. 86400 A 136.186.230.90" >> textfiles/stillneedanewname
echo "show" >> textfiles/stillneedanewname
echo "send" >> textfiles/stillneedanewname
nsupdate -k /usr/local/etc/namedb/Keys/Kzonea.+157+57916.key -v textfiles/stillneedanewname

Trying to write "<<<" in ksh

I am having trouble with the code below:
IFS=: read c1 c2 c3 c4 rest <<< "$line"
Don't get me wrong this code works good but it doesn't seem to be used for ksh. I basically need to write the same code without the "<<<". There is not much info on the "<<<" online. If anybody has any ideas it would be much appreciated.
EDIT:
Ok code is as follows for the entire portion of programming:
m|M)
#Create Modify Message
clear
echo " Modify Record "
echo -en '\n'
echo -en '\n'
while true
do
echo "What is the last name of the person you would like to modify:"
read last_name
if line=$(grep -i "^${last_name}:" "$2")
then
oldIFS=$IFS
IFS=:
set -- $line
IFS=$oldIFS
c1=$1
c2=$2
c3=$3
c4=$4
shift; shift; shift; shift
rest="$*"
echo -e "Last Name: $1\nFirst Name: $2\nState: $4"
while true
do
echo "What would you like to change the state to?:"
read state
if echo $state | egrep -q '^[A-Z]{2}$'
then
echo "State: $state"
echo "This is a valid input"
break
else
echo "Not a valid input:"
fi
done
echo -e "Last Name: $c1\nFirst Name: $c2\nState: $state"
echo "State value changed"
break
else
echo "ERROR: $last_name is not in database"
echo "Would you like to search again (y/n):"
read modify_choice
case $modify_choice in [Nn]) break;; esac
fi
done
;;
Ok so everything works except for the
echo -e "Last Name: $c1\nFirst Name: $c2\nState: $state"
It will just show:
Last Name:
First Name:
State:
So I can see it is not adding it to my echo correctly.
FINAL EDIT
CODE:
#Case statement for modifying an entry
m|M)
#Create Modify Message
clear
echo " Modify Record "
echo -en '\n'
echo -en '\n'
while true
do
echo "What is the last name of the person you would like to modify:"
read last_name
if line=$(grep -i "^${last_name}:" "$2")
then
echo "$line" |
while IFS=: read c1 c2 c3 c4 rest; do
echo -e "Last Name: $c1\nFirst Name: $c2\nState: $c4"
last=$c1
first=$c2
done
while true
do
echo "What would you like to change the state to?:"
read state
if echo $state | egrep -q '^[A-Z]{2}$'
then
echo "State: $state"
echo "This is a valid input"
break
else
echo "Not a valid input:"
fi
done
echo -e "Last Name: $last\nFirst Name: $first\nState: $state"
echo "State value changed"
break
else
echo "ERROR: $last_name is not in database"
echo "Would you like to search again (y/n):"
read modify_choice
case $modify_choice in [Nn]) break;; esac
fi
done
;;
A here string in Bash
command <<<"string"
is basically equivalent to
echo "string" | command
with the obvious exception that the latter uses a pipeline, which means you cannot meaningfully use it with read in particular. A common workaround is to use the set builtin to capture tokens from a string or an external command:
oldIFS=$IFS
IFS=:
set -- $line # no quotes
IFS=$oldIFS
c1=$1
c2=$2
c3=$3
c4=$4
shift; shift; shift; shift
rest="$*" # loses spacing / quoting
Another workaround is to use a loop which iterates just once; this may seem elegant at first, but can lead to rather clunky code if the body of the pseudo-loop is long or complex.
echo "$line" |
while IFS=: read c1 c2 c3 c4 rest; do
: stuff which uses those variables
done
This works around the problem that echo stuff | read variable will run read in a child process and thus immediately forget the value of variable -- the body of the while loop is all the same process in which the read happened, and so the values of the variables it initialized are visible inside the loop.
Another, similar workaround is to delegate the reading and procesIng to a function;
process () {
IFS=: read c1 c2 c3 c4 rest
: stuff which uses those variables
}
echo "$line" | process
Whether this is clunky or elegant depends a lot on what happens in the function. If it's neatly encapsulated, it can be rather attractive; but if you end up passing in a bunch of unrelated variables (or worse, modifying globals inside the function!) it can be quite the opposite.

unix shell script creating backup.sh

How to write a shell script named "backup.sh" which accepts one parameter, which would be a filename/directory.
Create a backup copy of that with the .bak appended to its name.Show message on success.
If the file/directory does not exist, show a proper message.
i did up to this point.please help me to figure this out
#!/bin/sh
#create_backup.sh
And add a .bak
bak="${backup.sh}.bak"
if [ "$#" -eq 0 ]
then
exit 1;
echo "File Succesfully backuped"
fi
cp ${1} "${1}.back"
echo "File is not found "
exit 0
#!/bin/bash -e
directory=$1
cp -r $directory $directory.bak
echo "Success"
obvious caveats with pathing/error codes/etc

Need to add logic in Unix script convert from Windows to Unix format

I am having one script need to add logic that if some one add file from winscp and do not convert into plain text while transfer. so some time we get some special character (^m) in some value, i wanted to remove them
Here is my code.
cd $HOME_DIR
if [ $SHELL_STEP = 'step2' ]; then
if [ -s $DATA_DIR/$DATA_FILE.txt ]; then echo "The data file is present." cat -v new_reguest.txt $ awk '{ sub("\r$", ""); print }' new_request.txt > new_request.txt echo "Data file $DATA_FILE.txt found in $DATA_DIR directory." >> $LOG_FILE echo "" >> $LOG_FILE STATUS='good' else echo "The data file has not arrived yet." fi
if [ $STATUS = 'bad' ]; then
echo "The data file not found."
echo "The data file not found." >> $LOG_FILE
echo "" >> $LOG_FILE
SHELL_STEP='step5'
else
SHELL_STEP='step3'
fi
fi
I tried to using awk command, but it's not looking good.
please assist.
Most implementations will provide tools like dos2unix or d2u to remove carriage returns from the end of lines. You can use something like:
dos2unix new_request.txt >new_request_2.txt
mv new_request_2.txt new_request.txt
If you don't have the dos2unix command, you can do the same thing with sed:
sed -i 's/\r$//' new_request.txt
The -i is for in-place editing. If your version of sed doesn't have that, you'll have to resort to the same temporary file trick used in the dos2unix code above.

How to use sed with user input

Script performs two things
1.Enables the user input a file name
2.Enables the user to input a line number to view the content
echo "Enter the file name"
read fname
find / -name "$fname" > /tmp/newone.txt
if test $? -eq 0
then
{
echo "File found"
echo "The no of line in the file $fname is `cat /tmp/newone.txt | wc|awk '{pri
nt $1}'`"
echo "Enter the line no"
read lcnt
sed '"$lcnt" p' "$fname"
}
else
{
echo "File not found"
}
fi
Issue
1.Getting error in the sed part
Error message "sed: -e expression #1, char 3: extra characters after command"
how to rectify it ?
2.Can i redirect the output of 'find' to a variable
For example
$flloc =/tmp/newone.txt
so i will be able to use '$flloc' instead of the absolute path
1) This is how you'd go about using your variable in the sed command:
echo "Line no: "
read lcnt
sed -n "$lcnt p" $fname
What was wrong with your original expression is that bash variables aren't interpreted when you use single quotes. Example:
lcnt=5
# prints $lcnt
echo '$lcnt'
# prints 5
echo "$lcnt"
2) To store your find output to a variable, simply do this:
floc=`find / -name $fname` # Here I'm using backticks, not single quotes.

Resources