I wrote a simple script which must show progress while user waiting. But I get infinitive loop and seems sleep not working. What wrong in this code?
#!/bin/bash
spinner=(
"Working "
"Working. "
"Working.. "
"Working... "
"Working...."
)
while sleep 10
do
for item in ${spinner[*]}
do
echo -en "\r$item"
sleep .1
echo -en "\r \r"
done
done
One idea:
using the bash (system) variable SECONDS to measure our 10 seconds
using a tput code for ovewriting a line
eliminating the spinner[] array (since the only difference in values is the number of trailing periods)
EraseToEOL=$(tput el)
max=$((SECONDS + 10)) # add 10 seconds to current count
while [ $SECONDS -le ${max} ]
do
msg='Waiting'
for i in {1..5}
do
printf "%s" "${msg}"
msg='.'
sleep .1
done
printf "\r${EraseToEOL}"
done
printf "\n"
A small change to OP's current code using the max/SECONDS approach:
spinner=(
"Working "
"Working. "
"Working.. "
"Working... "
"Working...."
)
max=$((SECONDS + 10))
while [[ ${SECONDS} -le ${max} ]]
do
for item in ${spinner[*]}
do
echo -en "\r$item"
sleep .1
echo -en "\r \r"
done
done
Use the in/decrement variable i to put out the array...
#!/bin/bash
countdown(){
spinner=(
"Working "
"Working. "
"Working.. "
"Working... "
"Working...."
)
i=4
if [ ${i} -lt 5 ]
then
while true
do
for i in ${i}
do
printf "%s \t" ${spinner[i]}
sleep .1
printf "\r"
sleep .1
if [ ${i} -eq 0 ]
then
# Here you can clean up or do what to do at zero count
printf "\n"
unset i
unset spinner
return 0 # Can be used in ${?} from parent bash
else
i=$((${i}-1))
fi
done
done
return 1 # Should never be executed
fi
}
# A funny cd ;-)
cd(){
countdown && printf "%s\n" "DONE changing to "${1} # Gives out if return is 0 (${?})
unset cd
cd ${1}
}
#
cd ~
My method of showing progress while sleeping in bash:
sleep 5 | pv -t
It probably can't get any simpler than that :)
Check out this spiner
Or from this project
Related
i have a question about simple shell script.
this is the source code of rand.sh below
#!/bin/bash
n=$(( RANDOM % 100 ))
if [[ n -eq 42 ]]; then
echo "Something went wrong"
>&2 echo "The error was using magic numbers"
exit 1
fi
echo "Everything went accrding to plan"
and i'm going to make a new shell script, let me call it quiz.sh.
quiz.sh should loop until n==42. if n==42, save the stdout("Something went wrong") and stderr("The error was using magic numbers")
and it finally terminated with printing out those stdout,stderr and Total execution count.
here is my quiz.sh
#!/bin/bash
cnt=0
while [[ "${n}" -ne 42 ]]
do
(( cnt = "${cnt}"+1 ))
source ./rand.sh &> error.txt
done
cat error.txt
echo "${cnt}"
but this is not working. because of exit 1 in rand.sh, the program is terminated before executing cat and echo which is at the end two line.
how can i fix it?? please let me know!
I want to make happen cat error.txt and echo "${cnt}" as well
Run the loop in a subshell
(
while something; do
something
exit 1 # exits only from the subshell
done
)
Note: parent shell doesn't access/inherit child process environment. So cnt is going to be empty in parent shell. Transfer it some other way.
(
cnt=0
while ((n != 42)); do
((cnt++))
echo "$cnt" > cntfile.txt
# >& is deprecated
source myrand > error.txt 2>&1
done
)
cnt=$(<cntfile.txt)
cat error.txt
echo "$cnt"
Reference Bash manual command grouping.
As KamilCuk pointed out correctly, you should use $n instead of n.
Furthermore, I personally would add that using source ./rand.sh &> error.txt is kind of weird in this case. If you want to run it as a background process, use:
./rand.sh &> error.txt &
wait $! # $! is the pid
Otherwise, just make a function out of it:
#!/bin/bash
function myrand {
n=$(( RANDOM % 100 ))
if [[ n -eq 42 ]]; then
echo "Something went wrong"
>&2 echo "The error was using magic numbers"
return 1
fi
echo "Everything went accrding to plan"
return 0
}
cnt=0
while [[ "${n}" -ne 42 ]]
do
(( cnt = "${cnt}"+1 ))
myrand() &> error.txt
done
cat error.txt
echo "${cnt}"
p.s. code not tested, but I guess it works.
I built my web server and I'm trying to do a test. So I simulate many requests with bash script:
i=0
while [ $i -lt 20 ]; do
echo ''
echo ''
echo ''
echo '============== current time ==============='
echo $i
echo '==========================================='
echo ''
curl -i http://www.example.com/index?key=abceefgefwe
i=$((i+1))
done
This works well but I prefer to make all of echo at the same position on the terminal.
I've read this: How to show and update echo on same line
So I add -ne for echo but it doesn't seem to work as expected.
The messages of curl can still push the echo away.
This is what I need:
============== current time =============== ---\
1 <------ this number keeps updating ----> the 3 lines stay here
=========================================== ---/
Here is the messages of `curl`, which are showing as normal way
There's another option, to position the cursor before you write to stdout.
You can set x and y to suit your needs.
#!/bin/bash
y=10
x=0
i=0
while [ $i -lt 20 ]; do
tput cup $y $x
echo ''
echo ''
echo ''
echo '============== current time ==============='
echo $i
echo '==========================================='
echo ''
curl -i http://www.example.com/index?key=abceefgefwe
i=$((i+1))
done
You could add a clear command at the beginning of your while loop. That would keep the echo statements at the top of the screen during each iteration, if that's what you had in mind.
When I do this sort of thing, rather than using curses/ncurses or tput, I just restrict myself to a single line and hope it doesn't wrap. I re-draw the line every iteration.
For example:
i=0
while [ $i -lt 20 ]; do
curl -i -o "index$i" 'http://www.example.com/index?key=abceefgefwe'
printf "\r==== current time: %2d ====" $i
i=$((i+1))
done
If you're not displaying text of predictable length, you might need to reset the display first (since it doesn't clear the content, so if you go from there to here, you'll end up with heree with the extra letter from the previous string). To solve that:
i=$((COLUMNS-1))
space=""
while [ $i -gt 0 ]; do
space="$space "
i=$((i-1))
done
while [ $i -lt 20 ]; do
curl -i -o "index$i" 'http://www.example.com/index?key=abceefgefwe'
output="$(head -c$((COLUMNS-28))) "index$i" |head -n1)"
printf "\r%s\r==== current time: %2d (%s) ====" "$space" $i "$output"
i=$((i+1))
done
This puts a full-width line of spaces to clear the previous text and then writes over the now-blank line with the new content. I've used a segment of the first line of the retrieved file up to a maximum of the line's width (counting the extra text; I may be one off somewhere). This would be cleaner if I could just use head -c$((COLUMNS-28)) -n1 (which would care about the order!).
I'm rather new to BASH and I was wondering how could I print 2 strings on the same 2 lines.
What I'm trying to do, is create a 2 line progress-bar in BASH.
Creating 1 line progress bar is rather easy, I do it like this:
echo -en 'Progress: ### - 33%\r'
echo -en 'Progress: ####### - 66%\r'
echo -en 'Progress: ############ - 100%\r'
echo -en '\n'
But now I'm trying to do the same thing but with 2 lines, and everything I tried failed so far.
In the second line, I want to put a "Progress Detail" that tells me at what point in the script it is, like for example: what variable is gathering, what function is it running. But I just can't seem to create a 2 line progress bar.
It's possible to overwrite double lines using tput and printf, for example:
function status() {
[[ $i -lt 10 ]] && printf "\rStatus Syncing %0.0f" "$(( i * 5 ))" ;
[[ $i -gt 10 ]] && printf "\rStatus Completing %0.0f" "$(( i * 5 ))" ;
printf "%% \n" ;
}
for i in {1..20}
do status
printf "%0.s=" $(seq $i) ;
sleep .25 ; tput cuu1 ;
tput el ;
done ; printf "0%%\n" ; printf " %.0s" {1..20} ; printf "\rdone.\n"
one-liner:
for i in {1..20}; do status ; printf "%0.s=" $(seq $i) ; sleep .25 ; tput cuu1 ; tput el ; done ; printf "0%%\n" ; printf " %.0s" {1..20} ; printf "\rdone.\n"
The loop calls the status function to display the appropriate text during a particular time.
The resulting output would be similar to:
Status Completing 70%
==============
You can use \033[F to go to previous line, and \033[2K to erase the current line (just in case your output length changes).
That's the script I did:
echo -en 'Progress: ### - 33%\r'
echo -en "\ntest" # writes progress detail
echo -en "\033[F\r" # go to previous line and set cursor to beginning
echo -en 'Progress: ####### - 66%\r'
echo -en "\n\033[2K" # new line (go to second line) and erase current line (aka the second one)
echo -en "test2" # writes progress detail
echo -en "\033[F\r" # go to previous line and set cursor to beginning
echo -en 'Progress: ############ - 100%\r'
echo -en "\n\033[2K" # new line and erase the line (because previous content was "test2", and echoing "test" doesn't erase the "2")
echo -en "test" # write progress detail
echo -en '\n'
I've got my script going as far as It can connect, login and run the command. But I'm stuck as how do I save the response from the command to a file, without saving the whole session.
#!/bin/sh
Var=1
while [ $Var -lt 20 ]
do
HOST='IPa.ddr.ess.'$Var
USER='MyUser'
PASSWD='MyPassword'
CMD='MyCommand'
(
echo open "$HOST"
sleep 1
echo "$USER"
sleep 1
echo "$PASSWD"
sleep 1
echo "$CMD"
#I want to save the output from my $cmd to an varaible $Output
#Then I want to write "$HOST - $Output" to a file named "output.txt"
sleep 2
echo "exit"
) | telnet
Var=$((Var + 1))
done
I'd appreciate any help, or pointers in the right direction
Ok, this looks more challenging than I initially thought. I like it :-)
#!/bin/sh
Var=1
while [ $Var -lt 20 ]
do
HOST='IPa.ddr.ess.'$Var
USER='MyUser'
PASSWD='MyPassword'
CMD='MyCommand'
MARKER='XXXX1234:AUIE'
(echo "$HOST - " ; (
echo unset echo
echo open "$HOST"
sleep 1
echo "$USER"
sleep 1
echo "$PASSWD"
sleep 1
echo echo "$MARKER"
echo "$CMD"
#I want to save the output from my $cmd to an varaible $Output
#Then I want to write "$HOST - $Output" to a file named "output.txt"
sleep 2
echo "exit"
) | telnet | sed -e "1,/$MARKER/d" ) >> output.txt
Var=$((Var + 1))
done
What this does is:
it disables echo-ing in telnet
After the login session, it prints a marker
anything after the marker is saved into output.txt
I imbricated into yet another shell that will print the "$HOST -" part
I'm trying to write a code which receives an integer "n" as a parameter and then print the n-th row of the Pascal's triangle starting from 0, 1,..,n.
for example if the entry is 3, the program prints 1 3 3 1.
So far I wrote a code to get the whole triangle printed, but I can't have just the last row.
This is what I have
echo "Insert the row:" read n for((i=0;i<$n;i++))
do
eval"a$i=($(w=1;v=1
for((j=0;j<$n-$i;j++))
do
[ $i -eq 0 -o $j -eq 0 ]&&{ v=1 && w=1; }||v=$((w+a$((i-1))[$((j))]))
echo -n "$v "
w=$v
done))"
eval echo "$(for((k=0;k<=$i;k++))
do
eval "echo -n \"\$((a\$((i-k))[k])) \""
done)"
done
#!/bin/bash
read -p "Insert the row:" n
typeset -A Tab
for((i=0;i<=$n;i++))
do
Tab[$i,0]=1
Tab[$i,$i]=1
for((j=1;j<$i;j++))
do
a=${Tab[$((i-1)),$((j-1))]}
b=${Tab[$((i-1)),$j]}
Tab[$i,$j]=$(( a + b ))
done
done
#print result
for((j=0;j<=$n;j++))
do
echo -n ${Tab[$n,$j]} " "
done
echo
Test :
Insert the row:3
1 3 3 1
I found an awk solution to that question:
awk -v line_num=5 'BEGIN{for(i=line_num;i<=line_num;i++){c=1;r=c;for(j=0;j<i;j++){c*=(i-j)/(j+1);r=r" "c};print r}}'
Change line_num value to the desired one.
Based on a solution found here.
That's of course if awk counts…
Here is a simple bash script to print pascal's triangle using simple for,if else and echo command:
echo "Enter number of rows : "
read rows
coef=1
for((i=0;i<rows;i++))
do
for((space=1;space<=rows-i; space++))
do
echo -n " "
done
for((j=0;j<=i;j++))
do
if [ $j -eq 0 -o $i -eq 0 ]
then
coef=1;
else
coef=$((coef*(i-j+1)/j))
fi
echo -n $coef " "
done
echo
done