unable to do ssh to remote server gettign banner error - shell

#!/bin/bash
FILE="${1}"
while read line; do
REMOTESERVER=`echo ${line} | cut -d"," -f1`
REMOTEUSER=`echo ${line} | cut -d"," -f2`
REMOTEPASS=`echo ${line} | cut -d"," -f3`
DBUSER=`echo ${line} | cut -d"," -f4`
DBPASS=`echo ${line} | cut -d"," -f5`
DBNAME=`echo ${line} | cut -d"," -f6`
output=`sshpass -p \'$REMOTEPASS\' ssh -o StrictHostKeyChecking=no -T $REMOTEUSER#$REMOTESERVER
sleep 5
sqlplus -s "$DBUSER/$DBPASS#$DBNAME " <<EOF
set heading off feedback off verify off
select Instance_name,host_name,version,startup_time,status from v\\$instance;
exit
EOF
echo $output >> dboutput.txt

Related

i dont know why it writed me 11:not found

it writes me "file name" 18:11 not found
line 18 is where the if statement starts
#!/bin/bash
westfunc() {
echo "please select 2 airports(example:jfk):\n"
read place
read place2
echo "\e[32m--------------------------------------------------------------------------------"
echo
echo " $place $place2"
de=`weather-util -m $place | grep "Temperature"| cut -d "T" -f2 | cut -d "e" -f4| cut -d ":" -f2`
we=`weather-util -m $place | grep "Wind" | cut -d "W" -f3 | cut -d ")" -f2`
sc=`weather-util -m $place | grep "Sky conditions" | cut -d "n" -f3 | cut -d ":" -f2`
var=`weather-util -m $place | grep "Temperature"| cut -d " " -f5 | cut -d "." -f1`
if (( $var > 5 ))
then
echo "hello"
fi
}
i tried to change $ the ( and stuff like that

Shell Script do while flow

I have a script whose content is like:
#!/bin/bash
DB_FILE='pgalldump.out'
echo $DB_FILE
DB_NAME=''
egrep -n "\\\\connect\ $DB_NAME" $DB_FILE | while read LINE
do
DB_NAME=$(echo $LINE | awk '{print $2}')
STARTING_LINE_NUMBER=$(echo $LINE | cut -d: -f1)
STARTING_LINE_NUMBER=$(($STARTING_LINE_NUMBER+1))
TOTAL_LINES=$(tail -n +$STARTING_LINE_NUMBER $DB_FILE | \
egrep -n -m 1 "PostgreSQL\ database\ dump\ complete" | \
head -n 1 | \
cut -d: -f1)
tail -n +$STARTING_LINE_NUMBER $DB_FILE | head -n +$TOTAL_LINES > /backup/$DB_NAME.sql
done
I know what it is doing. But i have a doubt about the flow of do while in this case. At line egrep -n "\\\\connect\ $DB_NAME" $DB_FILE | while read LINE will egrep runs first or while . Because DB_NAME is empty at start of code.
Could anyone please explain the flow of do while in this case.

How to hide error & warning bash colors in Xcode and Source Tree?

I have following bash script
while read -r line; do
FILEPATH=$(echo $line | cut -d : -f 1)
L=$(echo $line | cut -d : -f 2)
C=$(echo $line | cut -d : -f 3)
TYPE=$(echo $line | cut -d : -f 4 | cut -c 2-)
MESSAGE=$(echo $line | cut -d : -f 5 | cut -c 2-)
DESCRIPTION=$(echo $line | cut -d : -f 6 | cut -c 2-)
if [ "$TYPE" == 'error' ]; then
printf "\n \e[31m$TYPE\e[39m\n"
else
printf "\n \e[33m$TYPE\e[39m\n"
fi
printf " \e[90m$FILEPATH:$L:$C\e[39m\n"
printf " $MESSAGE - $DESCRIPTION\n"
done <<< "$RESULT"
In terminal colors works perfectly.
How to hide colors in Xcode & Source Tree?

how to read words from a file using shell script

i have a file /ws/$1-rcd/temp.txt which has only one line as follows
198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/...
i have a script to get the value repository/open_source/commons_collections and 3_2_2 by reading the file and looping through it using for loop
i have my code as follows
grep -n "$4" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f1,2 | sed -e 's/\:element/|/g' | sed -e 's/ //g' > /ws/$1-rcd/temp.txt
for i in `cat /ws/$1-rcd/temp.txt`
do
line=`echo $i | cut -d"|" -f1`
path=`echo $i | cut -d"|" -f2`
whoami
directory_temp=`echo $path | awk -F "/" '{ print $(NF-2)}'`
if [ "$directory_temp" == "$4" ]
then
OLD_VERSION=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
total_fields=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
dir_path=`expr ${total_fields} - 2`
loc=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${dir_path}"`
location=`echo $loc | cut -d"/" -f4,5,6`
fi
done
but when i run this code it gives me an error as
-bash: line 45: syntax error near unexpected token |'
-bash: line 45:for i in 198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/...'
can anyone please suggest what am i doing wrong
If you want to iterate through each line of a file, use while loop like below
while read -r line ;do
echo $line
done <file.txt
so, your code can be rewritten as
grep -n "$4" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f1,2 | sed -e 's/\:element/|/g' | sed -e 's/ //g' > /ws/$1-rcd/temp.txt
while read i ; do
line=`echo $i | cut -d"|" -f1`
path=`echo $i | cut -d"|" -f2`
whoami
directory_temp=`echo $path | awk -F "/" '{ print $(NF-2)}'`
if [ "$directory_temp" == "$4" ]
then
OLD_VERSION=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
total_fields=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
dir_path=`expr ${total_fields} - 2`
loc=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${dir_path}"`
location=`echo $loc | cut -d"/" -f4,5,6`
fi
done < /ws/$1-rcd/temp.txt
You may be better served relying on parameter expansion and substring removal. For example:
#!/bin/sh
a=$(<dat/lline.txt) ## read file into a
a=${a##*ccm_tpl/} ## remove from left to ccm_tpl/
num=${a##*collections/} ## remove from left to collections/
num=${num%%/*} ## remove from right to /
a=${a%%${num}*} ## remove from right to $num
Input File
$ cat dat/lline.txt
198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/..
Output
$ sh getvals.sh
a : repository/open_source/commons_collections/
num : 3_2_2
If you need to trim in some other way, just let me know and I'm happy to help further.

FTP to SFTP in shell scripting

This script is to connect to different servers and copy a file from a loaction defined.
It is mandatory to use sftp and not ftp.
#!/usr/bin/ksh -xvf
Detail="jyotibo|snv4915|/tlmusr1/tlm/rt/jyotibo/JyotiBo/ jyotibo|snv4915|/tlmusr1/tlm/rt/jyotibo/JyotiBo/"
password=Unix11!
c_filename=import.log
localpath1=`pwd`
for i in $Detail
do
echo $i
UserName=`echo $i | cut -d'|' -f1`
echo $UserName
remotehost=`echo $i | cut -d'|' -f2`
echo $remotehost
remote_path=`echo $i | cut -d'|' -f3`
echo $remote_path
{
echo "open $remotehost
user $UserName $password
lcd $localpath1
cd $remote_path
bi
prompt
mget $c_filename
prompt
"
} |ftp -i -n -v 2>&1
done
I want to do the similar thing using sftp instead of ftp.
The solution i got is below:
#!/usr/bin/ksh -xvf
Detail="jyotibo|snv4915|/tlmusr1/tlm/rt/jyotibo/JyotiBo/|import.log jyotibo|snv4915|/tlmusr1/tlm/rt/jyotibo/JyotiBo/|impor
t_1.log"
for i in $Detail
do
echo $i
remote_path=`echo $i | cut -d'|' -f3`
file_Name=`echo $i | cut -d'|' -f4`
echo "cd $remote_path" > .jyoti.batch.dat
echo "get $file_Name" >> .jyoti.batch.dat
echo "bye" >> .jyoti.batch.dat
UserName=`echo $i | cut -d'|' -f1`
echo $UserName
remotehost=`echo $i | cut -d'|' -f2`
echo $remotehost
sftp -b .jyoti.batch.dat $UserName#$remotehost
done
rm -f .jyoti.batch.dat

Resources