vendredi 27 février 2015

Bash: syntax error near unexpected token `else', grep termin using ssh

I'm new to bash, so this could be a simple fix. I'm trying to write a piece of code that allows me to check when jobs have completed on different servers. This is the current block of Code that I've got.



#!/bin/bash

boxes=(dm virgil alpha kolp law be corn zira)
Cyan='\033[0;36m'
NC='\033[0m' # No Color
for box in "${boxes[@]}"; do
echo ""
echo -e "${Cyan}${box}${NC}"
ssh "${box}" "ls -ltr | tail -n 11 | tr -s ' ' | cut -d ' ' -f 6- "
done

echo "------------------------------------------------------------------------------"


This works ok, and gives the following output.



dm
Jan 13 08:47 Model_SNAr_smallbasisset_TCM.chk
Jan 14 09:57 Phoscat_SS_regioazid_subopt.com
Jan 14 09:57 Phoscat_SS_regioazid_subopt.log
Jan 14 09:57 Phoscat_SS_regioazid_subopt.csh
Jan 14 09:57 nohup.out
Feb 4 17:57 Model_SNAr_smallbasisset_wb97xd_sm.com
Feb 4 17:57 Model_SNAr_smallbasisset_b3lyp_sm.com
Feb 4 17:57 Model_SNAr_medbasisset_b3lyp_sm.com
Feb 4 17:58 Model_SNAr_smallbasisset_wb97xd_fp.com
Feb 4 17:58 Model_SNAr_smallbasisset_b3lyp_fp.com
Feb 4 17:58 Model_SNAr_medbasisset_b3lyp_fp.com


As you can see, this shows all files, whereas I would like to narrow it down to just certain file extensions, and effectively "grep termin [filename]" in order to determine whether the job has completed normally. Currently my script will show any files that have been created, but not necessarily completed.


By changing



"ls -ltr | ..."


to



"ls -ltr *.log *.out |"


I can selectively search file extensions, but where I haven't got any files of that extension in a directory, I get the following which looks messy and I'd like to get rid of.



zira
ls: cannot access *.log: No such file or directory
ls: cannot access *.out: No such file or directory


To tidy this up, I tried to use an if..else statement, but got the following error.



#!/bin/bash

DIR="/$HOME"
boxes=(dm virgil alpha kolp law be corn zira)
Cyan='\033[0;36m'
NC='\033[0m' # No Color
for box in "${boxes[@]}"; do
ssh "${box}"
if [ "$(ls -A $DIR)" ]; then
for box in "${boxes[@]}"; do
echo ""
echo -e "${Cyan}${box}${NC}"
ssh "${box}" "ls -ltr *.log *.out | tail -n 11 | tr -s ' ' | cut -d ' ' -f 6-"
else
echo ""
fi

done

echo "------------------------------------------------------------------------------"


line 14: syntax error near unexpected token `else'
line 14: ` else'


Am I missing something easy, and how would I go about incorporating a "grep termin" style command?


Thanks,


Cal


Aucun commentaire:

Enregistrer un commentaire