Center Justify/align ASCII art in bash - bash

I was just working on my bash project and
I want the header ascii art to automatically adjust at the center whenever the terminal executes the script in whatever resolution. Is it possible mates?
Following is my code:
#!/bin/bash
clear
echo
echo -e "\t\t1▄██████▄#0000▄████████0000▄████████11▄██████▄ ";
echo -e "\t\t███0000███111███0110███111███1011███1███#0000██";
echo -e "\t\t███0001███111███0111███111███1100█▀11███#ffff██";
echo -e "\t\t███0010███11▄███▄▄▄▄██▀11▄███▄▄▄11111███#0000██";
echo -e "\t\t███0011███1▀▀███▀▀▀▀▀111▀▀███▀▀▀11111███#ffff██";
echo -e "\t\t███0100███1▀███████████111███1101█▄11███#0000██";
echo -e "\t\t███0101███111███1000███111███1110███1███#ffff██";
echo -e "\t\t1▀██████▀ffff███1001███111██████████11▀██████▀1";
echo -e "\n\n"

You can use the COLUMNS environment variable that returns the width of the terminal.
banner_width=46
indent=$(( (COLUMNS - banner_width) / 2 ))
prefix=''
for ((i=1; i<=indent; i++)) ; do
prefix+=' '
done
echo
echo -e "${prefix}1▄██████▄#0000▄████████0000▄████████11▄██████▄ ";

Related

Trying to create submit script to SGE

I'm trying to edit my working bash script to an SGE script in order to submit it as a job to the cluster.
Currently I have:
#!/bin/bash
# Perform fastqc on files in a specified directory.
for ((j=1; j <=17; j++))
do
directory=/data4/una/batch"$j"/
files=$""$directory"/*.fastq.gz"
batch=$"batch_"$j""
outfile=$""$batch"_submit_script.sh"
echo "#!/bin/bash">>$outfile;
echo "# Your job name">>$outfile;
echo "# -N $batch">>$outfile;
echo "# The job should be placed into the queue 'all.q'">>$outfile;
echo "#$ -q all.q">>$outfile;
echo "# Running in the current working directory">>$outfile;
echo "#$ -cwd">>$outfile;
echo "">>$outfile;
echo "# Export some necessary environment variables">>$outfile;
echo "#$ -S /bin/bash">>$outfile;
echo "#$ -v PATH">>$outfile;
echo "#$ -v LD_LIBRARY_PATH">>$outfile;
echo "#$ -v PYTHONPATH">>$outfile;
echo "# Finally, put your command here">>$outfile;
echo "">>$outfile;
echo "#$ for i in $files;">>$outfile;
echo "#$ do;">>$outfile;
echo "#$ fastqc -f fastq -o /data4/una/test/fastq/$i;">>$outfile;
echo "#$done">>$outfile;
echo "">>$outfile;
qsub $outfile;
done
But I'm getting an error:
Unable to read script file because of error: ERROR! invalid option argument "-f"
But
fastqc -f fastq -o /data4/una/test/fastq/$i
is a totally valid line in my bash script.
Thoughts?
Thanks!
It actually was poor formatting for my loop that was causing this error. I didn't need to start those lines with #$ at all, so those lines become:
echo "for i in $files;">>$outfile;
echo "do">>$outfile;
echo " fastqc -f fastq -o /data4/una/test/fastqc $i">>$outfile;
echo "done">>$outfile;
echo "">>$outfile;
qsub $outfile;

bash script overwrite current line completely

I have been playing with a script to test the speed of various VoIP servers by pinging, I then found a progress bar script and incorporated that... because cool!
now I'm trying to display the current server being tested below the status bar, I have the line overwriting but if the next server name is shorter it does not overwrite completely. I have tried various suggestions I've found but non work or they screw up my progress bar.
Im running osx but may also use this on various linux distros.
any suggestions would be great!
#!/bin/bash
HOSTS=("atlanta.voip.ms" "atlanta2.voip.ms" "chicago.voip.ms" "chicago2.voip.ms" "chicago3.voip.ms" "chicago4.voip.ms" "dallas.voip.ms" "denver.voip.ms" "denver2.voip.ms" "houston.voip.ms" "houstonnew1.voip.ms" "houstonnew2.voip.ms" "losangeles.voip.ms" "losangeles2.voip.ms" "newyork.voip.ms" "newyork2.voip.ms" "newyork3.voip.ms" "newyork4.voip.ms" "sanjose.voip.ms" "sanjose2.voip.ms" "seattle.voip.ms" "seattle2.voip.ms" "seattle3.voip.ms" "tampa.voip.ms" "tampanew1.voip.ms" "tampanew2.voip.ms" "washington.voip.ms" "washington2.voip.ms" "montreal.voip.ms" "montreal2.voip.ms" "montreal3.voip.ms" "montreal4.voip.ms" "toronto.voip.ms" "toronto2.voip.ms" "toronto3.voip.ms" "toronto4.voip.ms" "vancouver.voip.ms" "vancouver2.voip.ms" "amsterdam.voip.ms" "london.voip.ms" "melbourne.voip.ms" "paris.voip.ms")
Smallest="200000"
Server=""
tLen=${#HOSTS[#]}
# Slick Progress Bar
# Created by: Ian Brown (ijbrown#hotmail.com)
# Please share with me your modifications
# Functions
PUT(){ echo -en "\033[${1};${2}H";}
DRAW(){ echo -en "\033%";echo -en "\033(0";}
WRITE(){ echo -en "\033(B";}
HIDECURSOR(){ echo -en "\033[?25l";}
NORM(){ echo -en "\033[?12l\033[?25h";}
function showBar {
percDone=$(echo 'scale=2;'$1/$2*100 | bc)
halfDone=$(echo $percDone/2 | bc)
barLen=$(echo ${percDone%'.00'})
halfDone=`expr $halfDone + 6`
tput bold
PUT 7 28; printf "%4.4s " $barLen%
PUT 5 $halfDone; echo -e "\033[7m \033[0m"
tput sgr0
}
# Start Script
clear
HIDECURSOR
echo -e ""
echo -e ""
DRAW
echo -e " PLEASE WAIT WHILE SCRIPT IS IN PROGRESS"
echo -e " lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk"
echo -e " x x"
echo -e " mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj"
echo -e ""
echo -e ""
WRITE
for (( i=0; i<${tLen}; i++ ))
do
showBar $i ${tLen}
serl=${HOSTS[$i]}
seru=$(echo "$serl" | tr '[:lower:]' '[:upper:]')
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
#this line needs to overwrite completley
echo -ne "" '\r " TESTING:" $seru
Current1=` ping -c 4 -q -i .2 ${HOSTS[$i]} | grep avg | awk -F'/' '{print $5 }'`
Current=${Current1/./}
if [ "$Current" -lt "$Smallest" ]
then
Server=${HOSTS[$i]}
Smallest=$Current
fi
done
clear
Smallestd=$(echo "$Smallest" | sed 's/...$/.&/')
echo "Fastest Server = $Server # $Smallestd ms"
Here is an example of controlling clearing to end-of-line both during display of the meter, and in restoring the cursor after completion. I have reversed the loop to show the meter progressing from 100% (full) to 1% cleaning up after itself as it goes:
#!/bin/bash
## string of characters for meter (60 - good for 120 char width)
str='▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒'
tput civis # make cursor invisible
for i in `seq 100 -1 1`; do # for 1 to 100, save cursor, restore, output, restore
printf "\033[s\033[u Progress: %s %3d %% \033[u" "${str:0:$(((i+1)/2))}" "$i"
sleep 0.1 # small delay
done
sleep 0.5
printf "\033[K" # clear to end-of-line
tput cnorm # restore cursor to normal
exit 0
Note: cursor control and clearing during display of the meter is provided by the ANSI escapes for save cursor position and restore cursor position. After completion, clear to end of line is used to clean up. tput is used to control cursor visibility, but can also be used to save, restore and clear to end of line.
You can pad your with spaces so you always write the same number of characters. That way you'll overwrite the extra characters from before with spaces. For example you could something like
echo -en "\r"; echo -n $(printf " TESTING: %-40s" $seru)
Thanks to David C. Rankin I have an easy working answer
echo -ne "" '\r' " TESTING:" $seru '\033[K'

define "$key" use it in a script, but also store it

How can I create a specific line in another file using bash please? Like
echo "Please input the days you want to keep "
$key= ?
touch .beebrc; keep="$key"
where the file ".beebrc" has a line 'keep= x' and "$key" is created in the main script.
But how do I define "$key" please? And write it into ".beebrc" as a new line at position/line 8? The full function is -
function trim {
echo;
read -t "$temi" -n1 -p ""$bldgrn" Do you want to delete some of your download history? [y/n/q/r] $(tput sgr0)" ynqr ;
case "$ynqr" in
[Yy]) echo
read -t "$temi" -n3 -p ""$bldgrn" Please input the days you want to keep $(tput sgr0)" key ## ask
if test -e .beebrc && grep -q "^keep=" .beebrc 2>/dev/null ; then
sed -i "s/^keep=.*/keep=$key/" .beebrc
else
echo "keep=$key" >> .beebrc
#fi
cd /home/$USER/.get_iplayer
eval "$player" --trim-history "$key"; cd; ques;
#echo;;
[Nn]) ques;;
[Qq]) endex;;
[Rr]) exec "$beeb";;
* ) echo ""$bldgrn" Thank you $(tput sgr0)";;
esac
fi
};
Does this help in defining it all? (Sorry, should've put it in at first)
Perhaps:
read -p "Please input the days you want to keep: " key ## Ask.
echo "keep=\"$key\"" > .beebrc ## Store.
Use read to capture user input into a variable, and then write it to your file.
For example:
echo "Please input the days you want to keep "
read key
echo $key > .beebrc
#!/bin/bash
read -p "Please input the days you want to keep: " key
if test -e .beebrc && grep -q "^keep=" .beebrc 2>/dev/null ; then
sed -i "s/^keep=.*/keep=$key/" .beebrc
else
echo "keep=$key" >> .beebrc
fi
This script:
Prompts for input and stores the value in $key
Tests if .beebrc exists and that a line beginning "keep=" exists in it. If so, replace the keep= line with keep=$key
Otherwise append a new line/create the file with keep=$key.
This will need validation added because user input should not be trusted. (this answer might help)

Variables empty after inner loop

i have an issue with my bash script
t='Hostname\cfg;'
echo "Header" > $DST
for i in *
do
t="$i;"
egrep -v "(^$|^#)" $IPLIST | while read ii
do
if grep -q "$ii" $i
then
t=$t"y;"
else
t=$t"n;"
fi
echo "$t"
done
echo "x$t"
n=$(($n + 1))
echo "$n"
#echo "$ii;$t" # >> $DST
#t=""
done
Produces the following output:
h0010001.conf;y;
h0010001.conf;y;y;
<ommited>
h0010001.conf;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;n;n;y;y;y;y;y;y;y;y;y;y;y;y;n;y;y;y;y;y;y;n;y;y;y;y;y;y;y;y;y;n;n;
xh0010001.conf;
So for some reason the t Variable is empty after the inner loop has completed.
What I want to achieve is, to write t - after the second loop into a file.
#Barmar was spot on. Here's a typical workaround.
Change the while loop to run in the parent shell:
while read ii
do
if grep -q "$ii" $i
then
t=$t"y;"
else
t=$t"n;"
fi
echo "$t"
done < <(egrep -v "(^$|^#)" $IPLIST)

shell script to process a folder path

I am unable to process the following shell script
#!/bin/sh
P= '/mnt/temp/'
echo $P
Q= `echo $P` | sed -e "s/^.*\(.\)$/\1/"
echo 'Q is' $Q
echo ${P%?}
I expect the output as
/mnt/temp/
Q is /
/mnt/tmp
[edit]
As a next step I want to update remove the trailing / in P so am trying
if ${Q} ="/"
then
echo in
P=${P%?}
fi
but on execution it says
/: Permission denied
Spaces are not allowed in variable assignation in shell, so :
#!/bin/sh
P='/mnt/temp/'
echo "$P"
Q=$(echo "$P" | sed -e "s/^.*\(.\)$/\1/")
echo "Q is $Q"
echo "${P%?}"
And use more quotes, see http://mywiki.wooledge.org/Quotes http://mywiki.wooledge.org/Arguments and http://wiki.bash-hackers.org/syntax/words

Resources