bash: -c: line 9: syntax error near unexpected token `fi' - loops

I'm trying to write this script
It's read variables from csv file and make SSH connections thourgh these variables and then run an if condition inside the SSH.
#!/bin/bash
# ------------------------------------------
INPUT=Mnt.csv
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read rom alias port ver
do
ssh -n $alias "cd /opt/app;
echo ========================================================
find . -maxdepth 4 -type d -name '$rom' -print;
echo $port;
if [ ${ver} -eq 'v3' ]
then
cat /opt/app/v3
elif
cat /opt/app/v2
fi
;
exit"
done < $INPUT
IFS=$OLDIFS
It gives this error:
bash: -c: line 9: syntax error near unexpected token `fi'
bash: -c: line 9: ` fi'
Please can you help
Thanks

I've just corrected it like this anything work
"cd /opt/app;
echo ========================================================
find . -maxdepth 4 -type d -name '$rom' -print
echo $port
if [ ${ver} = "v3" ]
then
cat /opt/app/v3
else
cat /opt/app/v2
fi
exit"

Related

Bash array not passed out of loop

Am having this little bash script to build a custom ROM (see below)
Now the issue I encounter is the fact that I can't seem to be able to read the content of the array after the loop is done
While calling it before the new loop pass, all is good, however when the loop is done, I can not seem to be able to read it (seems to be empty)
I need to say that I am no bash expert and I usually do my code from out pal Google
What am I doing wrong here?
#!/bin/bash
#Build crDroid and optionally upload to FTP for all devices in devices.txt
# << Start configuration >>
#crDroid_path is the location of build environment root path aka {ANDROID_BUILD_TOP}
crDroid_path=~/crDroid
#set how much RAM is used by JACK if building with
RAM=12
#CCache size
ccachesize=30G
#set if you want to save changelog file to script_path (from where the script runs) at end of build (useful to add changelog info to forums and so on... easy to find)
copy_changelog=true
#FTP config
upload_build=true
FTP_hostname="ftp://domain.tld"
FTP_username=username
FTP_password=password
# << End configuration >>
# Specify colors utilized in the terminal
red=$(tput setaf 1) # red
grn=$(tput setaf 2) # green
ylw=$(tput setaf 3) # yellow
blu=$(tput setaf 4) # blue
cya=$(tput rev)$(tput bold)$(tput setaf 6) # bold cyan reversed
ylr=$(tput rev)$(tput bold)$(tput setaf 3) # bold yellow reversed
grr=$(tput rev)$(tput bold)$(tput setaf 2) # bold green reversed
txtrst=$(tput sgr0) # Reset
echo "==========================================="
echo "${cya}Initiate build script - v2.9 by Gabriel Lup (gwolfu#xda)"${txtrst}
echo "==========================================="
#detect path where the script is running
script_path="`dirname \"$0\"`" # relative
script_path="`( cd \"$script_path\" && pwd )`" # absolutized and normalized
if [ -z "$script_path" ] ; then
# error; for some reason, the path is not accessible
echo "${red}Can not read run path"
echo "Build can not continue"${txtrst}
exit 1 # fail
fi
#check if devices.txt exists
devices=$script_path/devices.txt
if [ ! -f $devices ]; then
echo "${red}devices.txt missing"
echo "Build can not continue"${txtrst}
exit 1 # fail
fi
#cleanup old changelog.txt
changelog=$script_path/changelog.txt
if [ -e $changelog ]; then
rm -f $changelog
fi
#check if already synced
crdroid_synced=$crDroid_path/vendor/lineage/config/crdroid.mk
if [ ! -f $crdroid_synced ]; then
echo "${ylw}Detected missing first sync... atempting first time sync..."${txtrst}
cd $crDroid_path
repo sync -f --force-sync --no-clone-bundle
fi
if [ ! -f $crdroid_synced ]; then
read -p "${red}Something went wrong :( - Maybe misconfigured script!?"
exit
fi
cd $crDroid_path
echo "${blu}Run sync?${txtrst}"
select yn in "Yes" "No"; do
case $yn in
Yes ) repo sync -f --force-sync --no-clone-bundle; break;;
No ) break;;
esac
done
echo "${blu}Make clean build?${txtrst}"
select yn in "Yes" "No"; do
case $yn in
Yes ) . build/envsetup.sh && make clean; break;;
No ) break;;
esac
done
#check if repopick.txt exists and execute commands from it
repopick_file=$script_path/repopick.txt
if [ -f $repopick_file ]; then
. build/envsetup.sh
cat $script_path/repopick.txt | while read line
do
$line
done
fi
INFO=()
#Set CCache size
$crDroid_path/prebuilts/misc/linux-x86/ccache/ccache -M $ccachesize
#detect android version based on crdroid.mk
rl2="`sed -n '2p' $crDroid_path/vendor/lineage/config/crdroid.mk`"
set -- "$rl2"
IFS=" "; declare -a android_major=($*)
rl3="`sed -n '3p' $crDroid_path/vendor/lineage/config/crdroid.mk`"
set -- "$rl3"
IFS=" "; declare -a android_minor=($*)
android=${android_major[2]}"."${android_minor[2]}
#detect crDroid version based on crdroid.mk
rl6="`sed -n '6p' $crDroid_path/vendor/lineage/config/crdroid.mk`"
set -- "$rl6"
IFS=" "; declare -a crDroid_version=($*)
crDroid=${crDroid_version[2]}
echo "==========================================="
echo "${ylr}Setting build environment"${txtrst}
echo "-------------------------------------------"
echo "Script path set to: "${grn}$script_path${txtrst}
echo "crDroid path set to: "${grn}$crDroid_path${txtrst}
echo "Jack RAM usage set to: "${grn}$RAM"GB RAM"${txtrst}
echo "Copy changelog to script path at end of build?: "${grn}$copy_changelog${txtrst}
echo "Upload complete build to FTP?: "${grn}$upload_build${txtrst}
echo "Trying to compile crDroid "${grn}$crDroid${txtrst}" based on Android "${grn}$android${txtrst}
echo "==========================================="
echo ""
echo "${ylw}Initiate build for all devices...${txtrst}" #described in devices.txt
cat $script_path/devices.txt | while read line
do
IFS=', ' read -r -a device_uploadpath <<< "$line"
#set device name
device=${device_uploadpath[0]}
if [[ $device == *"#"* ]]; then
device="${device//#}"
echo "${red}Found comment (#): Skipping build for ${ylw}$device ${red}${txtrst}"
INFO+=('1 '$device' (Reason: instruction to skip found in devices.txt)')
else
echo "${grn}Now building "${ylw}$device${txtrst}
#set BuildID - aka name of the zip file from OUT folder at the end of the build
BuildID="crDroidAndroid-"$android"-"$(date -d "$D" '+%Y')$(date -d "$D" '+%m')$(date -d "$D" '+%d')"-"$device"-v"$crDroid"-BETA.zip"
#Jack settings
echo "Adding "$RAM" RAM to JACK"
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx"$RAM"g"
./prebuilts/sdk/tools/jack-admin kill-server
./prebuilts/sdk/tools/jack-admin start-server
#initiate build script and start actual build
. build/envsetup.sh
brunch $device
#define upload file path
uploadfile=$crDroid_path/out/target/product/$device/$BuildID
echo "Checking build result status..."
if [ -e "$uploadfile" ]; then
echo "${grn}Seems compilation is ready!${txtrst}"
if [ "$upload_build" = true ] ; then
echo "${grn}Uploading new "$device" build to FTP server${txtrst}"
echo "${ylw}Creating folder over FTP if not exist${txtrst}"
curl $FTP_hostname/${device_uploadpath[1]}/ --user $FTP_username:$FTP_password --ftp-create-dirs
echo "${ylw}Uploading...${txtrst}"
curl -T $uploadfile $FTP_hostname/${device_uploadpath[1]}/ --user $FTP_username:$FTP_password
res=$?
if test "$res" != "0"; then
echo "${red}Upload of build for device $device failed with code $res${txtrst}"
INFO+=('1 '$device' (Reason: CURL error no.'$res' ,however compilation appears to be made)')
else
INFO+=('2 '$device)
fi
fi
if [ "$copy_changelog" = true ] ; then
echo "Copy changelog file to "$script_path
cp $crDroid_path/out/target/product/$device/system/etc/Changelog.txt $script_path/changelog.txt
fi
else
echo "${red}Device "$device "did not produce a proper build${txtrst}"
INFO+=('0 '$device' (Reason: unknown - better run a new build manually)')
fi
fi
done
echo ${grr}"Script finished with following results"${txtrst}
for i in "${INFO[#]}"
do
#echo $i
if [[ $i == *"0"* ]]; then
codename=${i//0}
echo "${red}Compilation error for device" $codename${txtrst}
elif [[ $i == *"1"* ]]; then
codename=${i//1}
echo "${ylw}Warning for device" $codename${txtrst}
else
codename=${i//2}
echo "${grn}Device" $codename "compiled and uploaded successfully :)${txtrst}"
fi
done
This is the part that I am lost
echo ${grr}"Script finished with following results"${txtrst}
for i in "${INFO[#]}"
do
#echo $i
if [[ $i == *"0"* ]]; then
codename=${i//0}
echo "${red}Compilation error for device" $codename${txtrst}
elif [[ $i == *"1"* ]]; then
codename=${i//1}
echo "${ylw}Warning for device" $codename${txtrst}
else
codename=${i//2}
echo "${grn}Device" $codename "compiled and uploaded successfully :)${txtrst}"
fi
done

Script terminates prematurely after do loop. Last "echo" is not executed.

I am looking to execute the following script below. The issue I am encountering is it will not execute anything after the do loop. It doesn't matter what I happen to have after the loop, it never executes, so I am def missing something somewhere.
Also, any suggestions on a more efficient way or writing this script? I am very new to the scripting environment and very open to better ways of going about things.
#!/bin/bash
# mcidas environment
PATH=$HOME/bin:
PATH=$PATH:/usr/sww/bin:/usr/local/bin:$HOME/mcidas/bin
PATH=$PATH:/home/mcidas/bin:/bin:/usr/bin:/etc:/usr/ucb
PATH=$PATH:/usr/bin/X11:/common/tool/bin:.
export PATH
MCPATH=$HOME/mcidas/data
MCPATH=$MCPATH:/home/mcidas/data
export MCPATH
#variables
basedir1="ftp://ladsweb.nascom.nasa.gov/allData/6/MOD02QKM" #TERRA
basedir2="ftp://ladsweb.nascom.nasa.gov/allData/6/MYD02QKM" #AQUA
day=`date +%j`
day1=`date +"%j" -d "-1 day"`
hour=`date -u +"%H"`
min=`date -u +"%m"`
year=`date -u +"%Y"`
segment=1
count=$(ls /satellite/modis_processed/ | grep -v ^d | wc -l)
count_max=25
files=(/satellite/modis_processed/*)
if [ $hour -ge "17" ]; then
workinghour="16"
echo "Searching for hour $workinghour"
url="${basedir2}/${year}/${day1}/MYD02QKM.A${year}${day1}.${workinghour}*.006.${year}*"
wget -r -nd --no-parent -nc -e robots=off -R 'index.*' -P /satellitemodis/ $url
#find /satellite/modis/ -type f -mmin -30 -exec cp "{}" /satellite/modis_processed/ \;
for files in /satellite/modis_processed/*
do
echo "The number used for the data file is ${count}"
echo "The number used for the image file is ${segment}"
export segment
export count
#Run McIDAS
mcenv <<- 'EOF'
imgcopy.k MODISD.${count} MODISI.${segment} BAND=1 SIZE=SAME
imgremap.k MODISD.${segment} MODISI.${segment} BAND=1 SIZE=ALL PRO=MERC
imgcha.k MODISI.${segment} CTYPE=BRIT
exit
EOF
segment=`expr ${segment} + 1`
count=`expr ${count} - 1`
#Reset Counter if equal or greater than 25
if [[ $segment -ge $count_max ]]; then
segment=1
fi
find /satellite/awips -type f -name "AREA62*" -exec mv "{}" /awips2/edex/data/manual/ \;
done;
echo "We have exported ${segment} converted modis files to EDEX."
fi
You have a here-document in that script. That here-document is not properly terminated. The end marker, EOF, needs to be in the first column, not indented at all.
If you indent it, it has to be with tabs, and the start of the here-document should be <<-'EOF'.
The effect of the wrongly indented EOF marker is that the rest of the script is read as the contents of the here-document.
As Charles Duffy points out, ShellCheck is your friend.

Check multiple files exist in directory

How to find multiple files present in a directory in ksh (onAIX)
I am trying below one:
if [ $# -lt 1 ];then
echo "Please enter the path"
exit
fi
path=$1
if [ [ ! f $path/cc*.csv ] && [ ! f $path/cc*.rpt ] && [ ! f $path/*.xls ] ];then
echo "All required files are not present\n"
fi
I am getting error like check[6]: !: unknown test operator //check is my file name.
what is wrong in my script. Could someone help me on this.
My simplest idea:
N=$(ls -1 *mask* 2>/dev/null | wc -l)
echo $N

getting error while looping through array in shell

I have written a script to ftp a set of files from my directory , but i am getting an error while trying to loop through an array.
#!/usr/bin/ksh
HOST='xxx.xxx.xxx.xxx'
USER='avio'
PASSWD='jun'
FILES[0]=D141203.T024413
FILES[1]=D150101.T012755
FILES[2]=D141203.T024418
echo 'no of files: ' ${#FILES[#]}
ftp -n -v $HOST << EOS
ascii
user $USER $PASSWD
for i in "${FILES[#]}"
do
get $i
done
bye
EOS
here is my o/p :
no of files: 3
ftp.sh[10]: i: 0403-009 The specified number is not valid for this command.
The for loop is not executed as a bash command; rather, it is passed as a string to the ftp command. Instead, use a pipe to feed the output of the command to ftp:
#!/usr/bin/ksh
HOST='xxx.xxx.xxx.xxx'
USER='avio'
PASSWD='jun'
FILES[0]=D141203.T024413
FILES[1]=D150101.T012755
FILES[2]=D141203.T024418
echo 'no of files: ' ${#FILES[#]}
{ echo "ascii"
echo "user $USER $PASSWD"
for i in "${FILES[#]}"; do
echo "get $i"
done
echo "bye"
} | ftp -n -v $HOST

how to split files from error.log file of apache server while reading file line by line continuously?

I have done like this but I am having trouble with shellscript I have written. I am confused with tail command functionality and also when I see output of error.log on terminal it shows lines with 'e' deleted from words.
I have written like this please guide me how can I get my problem solved. I want to read this error.log file line by line and during reading lines I want to split fixed number of lines to small files with suffix i.e log-aa,log-ab,... I did this using split command. After splitting I want to filter lines with GET or POST word in them using regex and store this filtered lines into new file. After this store gets completed I need to delete all these log-* files.
I have written like this:
enter code here
processLine(){
line="$#"
echo $line
$ tail -f $FILE
}
FILE="/var/log/apache2/error.log"
if [ "$1" == "/var/log/apache2/error.log" ]; then
FILE="/dev/stdin"
else
FILE="$1"
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2
fi
fi
#BAKIFS=$IFS
#IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
#sed -e 's/\[debug\].*\(data\-HEAP\)\:\/-->/g' error.log > /var/log/apache2/error.log.1
while read -r line
do
processLine $line
done
exec 0<&3
IFS=$BAKIFS
logfile="/var/log/apache2/error.log"
pattern="bytes"
# read each new line as it gets written
# to the log file
#tail -1 $logfile
tail -fn0 $logfile | while read line ; do
# check each line against our pattern
echo "$line" | grep -i "$pattern"
#sed -e 's/\[debug\].*\(data\-HEAP\)\:/-->/g' error.log >/var/log/apache2/error.log
split -l 1000 error.log log-
FILE2="/var/log/apache2/log-*"
if [ "$1" == "/var/log/apache2/log-*" ]; then
FILE2="/dev/stdin"
else
FILE2="$1"
if [ ! -f $FILE2 ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE2 ]; then
echo "$FILE: can not read"
exit 2
fi
fi
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE2"
while read -r line
do
processLine $line
echo $line >>/var/log/apache2/url.txt
done
exec 0<&3
IFS=$BAKIFS
find . -name "var/log/apache2/logs/log-*.*" -delete
done
exit 0
The below code deletes files after reading and splitting error.log but when I put tail -f $FILE it stops deleting files I want to delete log-* files after it reaches last line of error.log file:
enter code here
processLine(){
line="$#"
echo $line
}
FILE=""
if [ "$1" == "" ]; then
FILE="/dev/stdin"
else
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2
fi
fi
#BAKIFS=$IFS
#IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
while read -r line
do
processLine $line
split -l 1000 error.log log-
cat log-?? | grep "GET\|POST" > storefile
#tail -f $FILE
done
rm log-??
exec 0<&3
#IFS=$BAKIFS
exit 0
Your code seems unnecessarily long and complex, and the logic is unclear. It should not have been allowed to grow this big without working correctly.
Consider this:
split -l 1000 error.log log-
cat log-?? | grep "GET\|POST" > storefile
rm log-??
Experiment with this. If these three commands do what you expect, you can add more functionality (e.g. using paths, checking for the existence of error.log), but don't add code until you have this part working.

Resources