I need help on how to connect remote systems through ssh command in while loop of shell script. I was able to connect one remote system using ssh from shell script. Please find sample code snippet as given below…..
ssh "root#148.147.179.100" ARG1=$rpmFileName 'bash -s' <<'ENDSSH'
echo ">>Checksum ..."
md5sum /root/$ARG1
ENDSSH
When tried to run same piece of code within a loop getting the error "syntax error: unexpected end of file", which I couldn’t resolve.
But when placing the same piece of code in another script file and using that file in while loop of another script, is working.
Can anyone help me with some solution.
Please find entire code as given below...
#!/bin/sh
rpmFileName=""
file="serverIps.txt"
dir="/home/rtulluri/downloads/EVAT-1123/AxisTar";
numberOfIps=0
axisTarfileTarget='/var'
#This function checks whether given ip is valid or not
#returns 0 for valid ip, 1 for invalid ip
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
#echo "stat = $stat"
return $stat
}
#Check whether given file exists or not
if [ -s $file ]
then
echo "$file exists"
else
echo "$file doesn't exist"
echo "exiting ........"
exit
fi
IFS=,
echo "---------------"
while read sysType serverIp uid pwd
do
sysType="${sysType#"${sysType%%[![:space:]]*}"}" # remove leading whitespace characters
sysType="${sysType%"${sysType##*[![:space:]]}"}" # remove trailing whitespace characters
serverIp="${serverIp#"${serverIp%%[![:space:]]*}"}" # remove leading whitespace characters
serverIp="${serverIp%"${serverIp##*[![:space:]]}"}" # remove trailing whitespace characters
uid="${uid#"${uid%%[![:space:]]*}"}" # remove leading whitespace characters
uid="${uid%"${uid##*[![:space:]]}"}" # remove trailing whitespace characters
pwd="${pwd#"${pwd%%[![:space:]]*}"}" # remove leading whitespace characters
pwd="${pwd%"${pwd##*[![:space:]]}"}" # remove trailing whitespace characters
if [ -n "$serverIp" ]
then
valid_ip $serverIp
#Assign the return value to a variable
isValidIp=$?
else
isValidIp=1
fi
if [ $isValidIp -eq "0" ]
then
numberOfIps=$(( $numberOfIps + 1 ))
echo "$numberOfIps) $serverIp --> is valid"
if [ "$sysType" = "ebox" ]
then
echo "$serverIp is an eBox device.."
echo "About to pass $serverIp as argument to connct.sh"
#./connct.sh $serverIp
ssh "$address" ARG1=$rpmFileName 'bash -s' <<'ENDSSH'
echo ">>Checksum ..."
ENDSSH
fi
else
echo "$serverIp --> is invalid"
fi
echo ""
done < $file
You might try to use the '-n' ssh switch
e.g.:
for i in {1..7}
do
ssh -n myhost$i mycommand
done
Related
I have a very peculiar issue with a script that I have wrote today. I am trying to form an ip address from two variables namely url and port. I am getting the url value from a library script which echos 10.241.1.8 and the port number is 10000. Now if I concatenate both the url and the port into another variable ip, I get completely a strange result(:10000241.1.8). I have my code and its result below. Please help me with your suggestions to fix this.
clear
echo $(date +'%H:%M:%S')'>> "Sample Records" Script started...'
usage() {
echo ">> $ script.sh -ctoff 89 -env c -ns reporting -depPath /user/release/audit_prime_oozie"
echo "Usage: $ script.sh -ctoff <Cutoff number> -env <testing cluster. ex: s for staging,c,d,p and a> -ns <optional: hive namespace> -depPath <deployment path>"
}
# Function to validate if value of a parameter is not empty
validate () {
if [[ $flag != 1 ]]; then
if [[ $tmpVar == *"-"* ]] || [[ -z $tmpVar ]]; then
usage
exit 1
fi
fi
}
options=$#
if [[ -z $options ]]; then
usage
exit 1
fi
arguments=($options)
index=0
# Function to extract the parameter values
check (){
for x in $options
do
index=`expr $index + 1`
case $x in
-ctoff)
cutOff="${arguments[index]}"
tmpVar=$cutOff
validate $tmpVar
;;
-env)
env="${arguments[index]}"
tmpVar=$env
validate $tmpVar
;;
-ns)
ns="${arguments[index]}"
tmpVar=$ns
validate $tmpVar
;;
-depPath)
depPath="${arguments[index]}"
tmpVar=$depPath
validate $tmpVar
;;
esac
if [[ -z $ns ]];then
ns=reporting
fi
done
}
check $#
error_exit(){
echo "$1" 1>&2
exit 1
}
# Create the execution directory
user=$(id -u -n)
PWD=`pwd`
INSTALL_ROOT=$PWD
LOCAL_DIR="/tmp/$user/sample_duns"
if [[ ! -d $LOCAL_DIR ]]; then
mkdir -p $LOCAL_DIR
echo ">> Created local directory $LOCAL_DIR"
if [[ $? -ne 0 ]]; then
echo ">> Unable to create $LOCAL_DIR, writing to current folder $INSTALL_ROOT"
LOCAL_DIR=$INSTALL_ROOT
fi
fi
if [[ $(ls -A $LOCAL_DIR) ]]; then
echo ">> Removed the temp files from $LOCAL_DIR"
rm -r $LOCAL_DIR/*
fi
# create the file name
datestamp=$(date '+%Y%m%d%H')
outFile=sample_duns_$datestamp.txt
# Copy the contents from HDFS to Local directory
echo ">> Copying required files from HDFS"
hdfs dfs -copyToLocal $depPath/data-warehouse/config/server.properties $LOCAL_DIR || error_exit "Cannot copy files from HDFS! Exiting now.."
hdfs dfs -copyToLocal $depPath/data-warehouse/reporting/lib_getHiveServer2ip.sh $LOCAL_DIR || error_exit "Cannot copy files from HDFS! Exiting now.."
if [[ $? -ne 0 ]]; then
echo ">> Files missing. Exiting now.."
exit 1
fi
# Call the lib script to get appropriate hiveserver2 ip address from the supplied environment for beeline execution
echo ">> Reading the HiveServer2 ip"
chmod +x $LOCAL_DIR/lib_getHiveServer2ip.sh
url=$($LOCAL_DIR/lib_getHiveServer2ip.sh $env $LOCAL_DIR/server.properties)
echo url=$url
port=10000
echo ip=$url:$b
Here is my output from the terminal.
11:18:16>> "Sample Records" Script started...
>> Removed the temp files from /tmp/user/sample_duns
>> Copying required files from HDFS
>> Reading the HiveServer2 ip
url=10.241.1.8
:10000241.1.8
I am expecting the below result
ip=10.241.1.8:10000
Adding the lib_getHiveServer2ip.sh script below
. $2 # read properties file
if [[ $1 == "d" ]]; then
ip=$devHSer
elif [[ $1 == "c" ]]; then
ip=$crankHSer
elif [[ $1 == "s" ]]; then
ip=$stgHSer
elif [[ $1 == "p" ]]; then
ip=$prdHSer
elif [[ $1 == "a" ]]; then
ip=$alpHSer
else
echo ">> Invalid cluster ip encountered. Exiting now ..."
exit 1
fi
echo $ip
Your url variable contains a carriage return character for some reason. Check lib_getHiveServer2ip.sh for weirdness.
Pipe your echo output to hexdump to confirm.
Edit: looks like your properties file has bad line endings. Use the file utility to check.
I'm trying to delete a line that contains a string pass through an argument, but I can't get it to work. I'm on OSX 10.9.
sed -i '' '/$2/d' /etc/hosts
Shouldn't that work? It just keeps the file as is. Nothing changes. My command is sudo hosts remove junior.dev.
Here is my shell script:
#!/bin/sh
let $# || { echo No arguments supplied. Example: hosts add 192.168.2.2 mysite.dev; exit 1; }
if [ $1 = "add" ]; then
if [ -z "$2" ] || [ -z "$3" ]; then
echo "You must supply an IP address and a host name."
exit 1;
else
echo "$2\t$3" >> /etc/hosts
echo "Done."
fi
fi
if [ $1 = "remove" ]; then
if [ -z "$2" ]; then
echo "You must supply a host name."
exit 1;
else
sed -i '' '/$2/d' /etc/hosts
echo "Done."
fi
fi
Use double-quotes ":
$ echo "$foo"
> bar
$ echo '$foo'
> $foo
When using double-quotes ", the variables are expanded, when using single-quotes ', they are not expanded.
I am trying to perform this:
i have a test file which md5sum of files located on sftp.
variables should contain an md5sum (string), if the variable is empty it means there is no file on the sftp server.
i am trying this code but it does not work..
if [ -z $I_IDOCMD5 ] || [ -z $I_LEGALMD5 ] || [ -z $I_ZIPMD5 ]
then
echo "ERROR: At least one file not present of checksum missing no files will be deleted" >>$IN_LOG
ERRORS=$ERRORS+2
else
if [[ $I_IDOCMD5 == $($DIGEST -a md5 $SAPFOLDER/inward/idoc/$I_IDOC) ]]
then
echo "rm IDOC/$I_IDOC" >/SAP/commands_sftp.in
else
echo "problem with checksum"
ERRORS=$ERRORS+2
fi
if [[ $I_LEGALMD5 == $($DIGEST -a md5 $SAPFOLDER/inward/legal/$I_LEGAL) ]]
then
echo "rm LEGAL/$I_LEGAL" >>/SAP/commands_sftp.in
else
echo "problem with checksum"
ERRORS=$ERRORS+2
fi
if [[ $I_ZIPMD5 == $($DIGEST -a md5 $SAPFOLDER/inward/zip/$I_ZIP) ]]
then
echo "rm ZIP/$I_ZIP" >>/SAP/commands_sftp.in
else
echo "problem with checksum"
ERRORS=$ERRORS+2
fi
The answer I prefer is following
[[ -z "$1" ]] && { echo "Parameter 1 is empty" ; exit 1; }
Note, don't forget the ; into the {} after each instruction
One way to check if a variable is empty is:
if [ "$var" = "" ]; then
# $var is empty
fi
Another, shorter alternative is this:
[ "$var" ] || # var is empty
In bash you can use set -u which causes bash to exit on failed parameter expansion.
From bash man (section about set builtin):
-u
Treat unset variables and parameters other than the special parameters "#" and "*" as an error when performing parameter
expansion. If expansion is attempted on an unset variable or
parameter, the shell prints an error message, and, if not interactive,
exits with a non-zero status.
For more information I recommend this article:
http://redsymbol.net/articles/unofficial-bash-strict-mode/
You can use a short form:
FNAME="$I_IDOCMD5"
: ${FNAME:="$I_LEGALMD5"}
: ${FNAME:="$I_ZIPMD5"}
: ${FNAME:?"Usage: $0 filename"}
In this case the script will exit if neither of the I_... variables is declared, printing an error message prepended with the shell script line that triggered the message.
See more on this in abs-guide (search for «Example 10-7»).
First test only this (just to narrow it down):
if [ -z "$I_IDOCMD5" ] || [ -z "$I_LEGALMD5" ] || [ -z "$I_ZIPMD5" ]
then
echo "one is missing"
else
echo "everything OK"
fi
echo "\"$I_IDOCMD5\""
echo "\"$I_LEGALMD5\""
echo "\"$I_ZIPMD5\""
"if the variable is empty it means there is no file on the sftp server"
If there is no file on the sftp server, is the variable then really empty ?
No hidden spaces or anything like that ? or the number zero (which counts as non-empty) ?
Aside from running every code path that has an NSLocalizedString in it, is there a way to verify that all NSLocalizedStrings have a key that actually exists in all your Localizable.strings files of all your bundles?
E.g. there wasn't a typo in one key such that NSLocalizedString won't find the key it's looking for?
OK I wrote a bash script to accomplish the above. Here it is. It took me hours so don't forget to up-vote me if you like. Feel free to make improvements, etc. I added a few comments suggesting potential improvements.
#!/bin/sh
# VerNSLocalizedStrings
while getopts "vsl:" arg; do
case $arg in
v)
verbose="yes"
;;
s)
stopOnMissing="yes"
;;
l)
lang=$OPTARG
;;
esac
done
if [[ -z $lang ]]
then
lang="en"
fi
searchDir=$lang.lproj
fileFound=`ls . | grep $searchDir`
if [[ -z $fileFound ]]
then
echo "dir "$searchDir" not found."
exit
fi
fileFound=`ls $searchDir/ | grep strings`
if [[ -z $fileFound ]]
then
echo "No .strings files found in dir "$searchDir"."
exit
fi
echo "Verifying NSLocalizationStrings in "$searchDir
# Get all the NSLocalizedString Commands
output=$(grep -R NSLocalizedString . --include="*.m")
# Go thru the NSLocalizedString commands line for line
count=$(( 0 ))
missing=$(( 0 ))
unusable=$(( 0 ))
OIFS="${IFS}"
NIFS=$'\n'
IFS="${NIFS}"
for LINE in ${output} ; do
IFS="${OIFS}"
# Now extract the key from it
# admittedly this only works if there are no line breaks between
# NSLocalizedStrings and the entire key,
# but it accounts for the keys it couldn't identify.
quotes=`echo $LINE | awk -F\" '{ for(i=2; i<=NF; i=i+2){ a = a"\""$i"\"""^";} {print a; a="";}}'`
key=`echo $quotes | cut -f1 -d"^"`
# If we couldn't find the key then flag problem
if [[ -z $key ]]
then
(( unusable += 1 ))
echo "Couldn't extract key: " $LINE
if [ -n "$stopOnMissing" ]
then
break
else
continue
fi
fi
# I don't know how grep works regarding length of string, only that
# if the string is too long then it doesn't find it in the file
keyLength=$(echo ${#key})
if [ $keyLength -gt 79 ]
then
(( unusable += 1 ))
echo "Key too long ("$keyLength"): " $key
if [ -n "$stopOnMissing" ]
then
break
else
continue
fi
fi
# It would be nice if this were a regular expression that allowed as many
# spaces as you want, even a line break then forced the quotes on the
# other side of the equal sign.
keyString=$key" ="
# Search for the key
found=$(iconv -sc -f utf-16 -t utf8 $searchDir/*.strings | grep "$keyString")
# damned if I know why some strings files are utf-16 and others are utf8
if [[ -z $found ]]
then
found=$(grep -r "$keyString" $searchDir/ --include=*.strings)
fi
# analyze the result
if [[ -z $found ]]
then
(( missing += 1 ))
echo "Missing: " $key "\n from: " $LINE
if [ -n "$stopOnMissing" ]
then
break
fi
else
if [ -n "$verbose" ]
then
echo "found: " $key
fi
fi
(( count += 1 ))
IFS="${NIFS}"
done
IFS="${OIFS}"
# It would also be nice if it went the other way and identified
# extraneous unused items in the strings files. But
# I've spent enough time on this for now
echo $count " keys analyzed"
echo $unusable " keys could not be determined"
echo $missing " keys missing"
To verify that all NSLocalizedStrings have a key that actually exists in all your Localizable.strings files or you missed localised you can enable the Localization enable "Show non-localized strings" option in the your project scheme editor.
Now run application you will see console logs for the missing localised string.
So I have this block of code. Basically, I'm taking file $i, checking if it's got content or not, checking if I can read it, if I can open it, grab the first line and see if it's a bash file. When I run this every time on a non-empty file, it was registers as true and echo's bash.
## File is empty or not
if [[ -s $i ]]
then
## Can we read the file
if [[ -r $i ]]
then
## File has content
if [[ $(head -n 1 $i) = "#! /bin/bash" ]]
then
echo -n " bash"
fi
fi
else
## file does not have content
echo -n " empty"
fi
This is what does the check of if it's bash:
if [[ $(head -n 1 $i) = "#! /bin/bash" ]]
Replace [[ with [ and enclose $(head -n 1 $i) in quotes.
[[ is itself an operator that tests its contents.