linux printf to specified terminal line - linux

In yocto, when I use bibake to build one recipe, stages related with this recipe will be printed in multiple lines, very beautiful.
So I want implement a tiny example like it.
get current cursor's row
run 2 threads, one outputs something at row + 1, another outputs
something at row + 2
As a result, I failed. Then I run 2 process which do same things, failed again.
Can some one give me some suggestions?
What I want is like:
ts:/home/test$ ./program1 &; ./program1 &
program1's output.....................
program2's output.....................

In Bash:
#!/usr/bin/env bash
printf '\n\n'
echo -ne "\033[6n"
read -rs -d\[ _
read -rs -dR foo
cursor_pos=$(cut -d";" -f1 <<< "$foo")
upper_row=$((cursor_pos - 2))
lower_row=$((cursor_pos - 1))
echo -ne "\033[${upper_row};0H"
echo upper row
sleep 2
echo -ne "\033[${lower_row};0H"
echo lower low
sleep 2
echo -ne "\033[${upper_row};0H"
printf "\r\e[0K%s\n" "upper again"
sleep 2
echo -ne "\033[${lower_row};0H"
printf "\r\e[0K%s\n" "lower again"

Related

bash/ksh grep script take more than one argument

#!/bin/ksh
if [ -n "$1" ]
then
if grep -w -- "$1" codelist.lst
then
true
else
echo "Value not Found"
fi
else
echo "Please enter a valid input"
fi
This is my script and it works exactly how I want at the moment, I want to add if I add more arguments It will give me the multiple outputs, How can I do that?
So For Example I do ./test.sh apple it will grep apple in codelist.lst and Give me the output : Apple
I want to do ./test.sh apple orange and will do:
Apple
Orange
You can do that with shift and a loop, something like (works in both bash and ksh):
for ((i = $#; i > 0 ; i--)) ; do
echo "Processing '$1'"
shift
done
You'll notice I've also opted not to use the [[ -n "$1" ]] method as that would terminate the loop early with an empty string (such as with ./script.sh a b "" c stopping without doing c).
To iterate over the positional parameters:
for pattern in "$#"; do
grep -w -- "$pattern" codelist.lst || echo "'$pattern' not Found"
done
For a more advanced usage, which only invokes grep once, use the -f option with a shell process substitution:
grep -w -f <(printf '%s\n' "$#") codelist.lst

Formatting with printf linux

I have a script below,
count=0
max=5
for i in {1..5}
do
sleep 1
count=`expr $count + 1`
printf "%s\r" "$i/$max completed"
done
echo "...OK"
The output of above script is below
(1 to 5 changing in place)/5 completed. then the line is replaced like "...OKompleted"
Output which I want is
(1 to 5 changing in place)/5...OK
Any suggestions on how to achieve this?
OS: CentOS7
\r only puts you at the beginning of the line. If you want to erase the line, you'll need some ANSI escape sequence, or you just use spaces to overwrite those characters.
You can try this:
count=0
max=5
for i in {1..5}
do
sleep 1
count=`expr $count + 1`
echo -en "\\r$i/$max completed"
done
echo "...OK"

Bash concurrent jobs gets stuck

I've implemented a way to have concurrent jobs in bash, as seen here.
I'm looping through a file with around 13000 lines. I'm just testing and printing each line, as such:
#!/bin/bash
max_bg_procs(){
if [[ $# -eq 0 ]] ; then
echo "Usage: max_bg_procs NUM_PROCS. Will wait until the number of background (&)"
echo " bash processes (as determined by 'jobs -pr') falls below NUM_PROCS"
return
fi
local max_number=$((0 + ${1:-0}))
while true; do
local current_number=$(jobs -pr | wc -l)
if [[ $current_number -lt $max_number ]]; then
echo "success in if"
break
fi
echo "has to wait"
sleep 4
done
}
download_data(){
echo "link #" $2 "["$1"]"
}
mapfile -t myArray < $1
i=1
for url in "${myArray[#]}"
do
max_bg_procs 6
download_data $url $i &
((i++))
done
echo "finito!"
I've also tried other solutions such as this and this, but my issue is persistent:
At a "random" given step, usually between the 2000th and the 5000th iteration, it simply gets stuck. I've put those various echo in the middle of the code to see where it would get stuck but it the last thing it prints is the $url $i.
I've done the simple test to remove any parallelism and just loop the file contents: all went fine and it looped till the end.
So it makes me think I'm missing some limitation on the parallelism, and I wonder if anyone could help me out figuring it out.
Many thanks!
Here, we have up to 6 parallel bash processes calling download_data, each of which is passed up to 16 URLs per invocation. Adjust per your own tuning.
Note that this expects both bash (for exported function support) and GNU xargs.
#!/usr/bin/env bash
# ^^^^- not /bin/sh
download_data() {
echo "link #$2 [$1]" # TODO: replace this with a job that actually takes some time
}
export -f download_data
<input.txt xargs -d $'\n' -P 6 -n 16 -- bash -c 'for arg; do download_data "$arg"; done' _
Using GNU Parallel it looks like this
cat input.txt | parallel echo link '\#{#} [{}]'
{#} = the job number
{} = the argument
It will spawn one process per CPU. If you instead want 6 in parallel use -j:
cat input.txt | parallel -j6 echo link '\#{#} [{}]'
If you prefer running a function:
download_data(){
echo "link #" $2 "["$1"]"
}
export -f download_data
cat input.txt | parallel -j6 download_data {} {#}

Pascal's triangle in Linux shell script

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

looking for a command to tentatively execute a command based on criteria

I am looking for a command (or way of doing) the following:
echo -n 6 | doif -criteria "isgreaterthan 4" -command 'do some stuff'
The echo part would obviously come from a more complicated string of bash commands. Essentially I am taking a piece of text from each line of a file and if it appears in another set of files more than x (say 100) then it will be appended to another file.
Is there a way to perform such trickery with awk somehow? Or is there another command.. I'm hoping that there is some sort of xargs style command to do this in the sense that the -I% portion would be the value with which to check the criteria and whatever follows would be the command to execute.
Thanks for thy insight.
It's possible, though I don't see the reason why you would do that...
function doif
{
read val1
op=$1
val2="$2"
shift 2
if [ $val1 $op "$val2" ]; then
"$#"
fi
}
echo -n 6 | doif -gt 3 ls /
if test 6 -gt 4; then
# do some stuff
fi
or
if test $( echo 6 ) -gt 4; then : ;fi
or
output=$( some cmds that generate text)
# this will be an error if $output is ill-formed
if test "$output" -gt 4; then : ; fi

Resources