J to print out status bar and percentage - j

I am calculating the processing time by putting
start =: 6!:0 ''
at the top of my code and putting
end =: (6!:0 '') - start
at the end of my code. However, is there any way that I can implement a status bar like this in J?
[==== ] 25%
[============ ] 50%
[==========================] 100%
Thank you!

If I understand your question, you could use smoutput defined as 0 0 $ 1!:2&2 by the system to display your processing milestones on the screen
someverb =: 3 : 0
smoutput '{ }'
code
smoutput '{+++ }'
more code
smoutput '{+++++ }'
more code
smoutput '{++++++++}'
)
but you would have to know that the places that you insert the smoutput expressions would correspond to the amount of processing that had taken place.
As an example:
test =: 3 : 0
​smoutput 6!:0 'hh:mm:ss.sss'
​6!:3 (2) NB. 2 second delay
​smoutput 6!:0 'hh:mm:ss.sss'
​6!:3 (2) NB. 2 second delay
​smoutput 6!:0 'hh:mm:ss.sss'
​)
test ''
14:53:42.313
14:53:44.317 NB. after two second delay
14:53:46.326 NB. after two second delay
or closer to the output you would like
test1 =: 3 : 0
start=. 6!:0 ''
smoutput '[ ] 0%'
6!:3 (2) NB. 2 second delay
smoutput '[=== ] 25%'
6!:3 (2) NB. 2 second delay
smoutput '[====== ] 50%'
6!:3 (4) NB. 4 second delay
smoutput '[============] 100%'
(6!:0 '')- start
)
test1 ''
[ ] 0%
[=== ] 25%
[====== ] 50%
[============] 100%
0 0 0 0 0 8.01821

Related

getting ./coundown 5 to countdown from 5

I have a file named countdown on my computer and I am trying to get it to countdown from what ever the user puts in.
for example ./countdown 5 would cause a 5 second timer to start outputting a "." every second and prints done after 5 seconds.
./countdown 10 would cause a 10 second timer to start outputting "." every second and prints done after 10 seconds.
here is my code, how can i read what the user inputs
t=$((5))
while [ $t -gt 0 ]; do
echo -ne "."
sleep 1
: $((t--))
done
echo "done"
Just change t=$((5)) to t=$1, which will assign the first argument to t.

Write a bash script that accepts a time duration as arguments?

I'm looking for a bash script that can parse a time duration.
If three arguments are given, they represent hours, minutes, and seconds. If two arguments are given, they represent minutes and seconds, with the hours zero.
What about the following:
#!/bin/bash
h=0
if [ "$#" -ge 3 ]
then
h=$1
shift
fi
sec=$((3600*$h+60*$1+$2))
echo "The total number of seconds is $sec"
Since the question does not specify what you aim to do with the given time, the program calculates the total number of seconds. Furthermore perhaps it is useful to do a check if at least two arguments are given.
The script uses the shift operation, the shift makes makes $1 := $2; $2 := $3, etc. In other words, the first argument is processed, and then you "pretend" it never existed.
By default you set h to zero, and only if the number of arguments is greater than or equal to 3, it will set h.
This is a more or less general solution for that type of task. Sorry, if it is a monkeycode, but I think it is sufficient:
gettime() {
params=(
years months weeks days hours minutes seconds
)
for i in `seq ${#params}`; do
param_i=$((${#params} - i + 1)) # reversed params index
[ $i -le $# ] && {
eval "local ${params[$param_i]}=\$$(($# - i + 1))"
} || {
eval "local ${params[$param_i]}=0"
}
eval "echo ${params[$param_i]} '==' \$${params[$param_i]}" # debug output
done
}
Here's the sample output:
$ gettime 3 4 5 6 7
seconds == 7
minutes == 6
hours == 5
days == 4
weeks == 3
months == 0
years == 0
Note, that the shell you are using must be not only support POSIX standards, but also arrays.
First Argument: $1
Second Argument: $2
Third Argument: $3
and so on...
Example:
bash-2.05a$ ./parseDuration.sh 13 25 25
13 hours and 25 minutes and 25 seconds
bash-2.05a$ cat ./parseDuration.sh
#!/bin/bash
echo "$1 hours and $2 minutes and $3 seconds"

Bash, print 0 in terminal each time a non recognised argument is input

I have a bash program which extracts marks from a file that looks like this:
Jack ex1=5 ex2=3 quiz1=9 quiz2=10 exam=50
I want the code to execute such that when I input into terminal:
./program -ex1 -ex2 -ex3
Jack does not have an ex3 in his data, so an output of 0 will be returned:
Jack 5 3 0
how do I code my program to output 0 for each unrecognized argument?
If I understand what you are trying to do, it isn't that difficult. What you need to do is read each line into a name and the remainder into marks. (input is read from stdin)
Then for each argument given on the command line, check if the first part matches the beginning of any grade in marks (the left size of the = sign). If it does, then save the grade (right side of the = sign) and set the found flag to 1.
After checking all marks against the first argument, if the found flag is 1, output the grade, otherwise output 0. Repeat for all command line arguments. (and then for all students in file) Let me know if you have questions:
#!/bin/bash
declare -i found=0 # initialize variables
declare -i grade=0
while read -r name marks; do # read each line into name & marks
printf "%s" "$name" # print student name
for i in "$#"; do # for each command line argument
found=0 # reset found (flag) 0
for j in $marks; do # for each set of marks check for match
[ $i = -${j%=*} ] && { found=1; grade=${j#*=}; } # if match save grade
done
[ $found -eq 1 ] && printf " %d" $grade || printf " 0" # print grade or 0
done
printf "\n" # print newline
done
exit 0
Output
$ bash marks_check.sh -ex1 -ex2 -ex3 < dat/marks.txt
Jack 5 3 0

Cascade in Rebol

In Logo Language, cascade is a procedure to to compose a function with itself several times (it is almost like fold in functional language).
Example:
add 4 add 4 add 4 5 --> cascade 3 [add 4 ?1] 5 == 17
2^8 --> cascade 8 [?1 * 2] 1
fibonacci 5 --> (cascade 5 [?1 + ?2] 1 [?1] 0)
factorial 5 --> (cascade 5 [?1 * ?2] 1 [?2 + 1] 1)
General notation for multi-input cascade, in Logo:
(cascade how many function1 start1 function2 start2 ...) with:
function1 -> ?1 ,
function2 -> ?2 ...
Cascade returns the final value of ?1.
In Rebol:
cascade1: func [howmany function1 start1] [....]
cascade2: func [howmany function1 start1 function2 start2] [....]
How to write cascade1 and cascade2 in Rebol ?
My answer uses REBOL 3, but could be backported to 2 without too much trouble. (I'd have done it in REBOL 2, but I don't have REBOL 2 on my system and haven't used it in a long time.) This implements cascade fully (i.e., with any number of "functions") and does it in an idiomatically REBOL kind of way: It uses a simple DSL.
cascade: funct [
count [integer!]
template [block!]
/only "Don't reduce TEMPLATE"
/local arg fun-block
][
param-list: copy []
param-number: 1
arg-list: copy []
fun-list: copy []
template-rules: [
some [
copy fun-block block! (
append param-list to word! rejoin ["?" ++ param-number]
append fun-list fun-block
)
copy arg any-type! (
append arg-list :arg
)
]
end
]
unless only [template: reduce template]
unless parse template template-rules [
do make error! rejoin ["The template " mold/flat template " contained invalid syntax."]
]
while [! tail? fun-list] [
fun-list: change fun-list func param-list first fun-list
]
fun-list: head fun-list
loop count [
temp-args: copy []
for f 1 length? fun-list 1 [
append/only temp-args apply pick fun-list f arg-list
]
arg-list: copy temp-args
]
first arg-list
]
Using it is simple:
print cascade 23 [[?1 + ?2] 1 [?1] 0]
This correctly gives the value 46368 from one of the cascade examples given in the Logo cascade documentation linked by the questioner. The syntax of the DSL should be brutally obvious. It's a series of blocks followed by starting arguments. The outer block is reduced unless the /only refinement is used. A block itself will work just fine as an argument, e.g.,
cascade 5 [[?1] [1 2 3]]
This is because the first block is interpreted as a "function", the second as a starting argument, the third as a "function" and so on until the template block is exhausted.
As far as I can tell, this is a complete (and rather elegant) implementation of cascade. Man, I love REBOL. What a shame this language didn't take off.
With bind, that Binds words to a specified context (in this case local context of function), and compose function, I get:
cascade: func [
times
template
start
] [
use [?1] [
?1: start
template: compose [?1: (template)]
loop times bind template '?1
?1
]
]
cascade 8 [?1 * 2] 1
== 256
cascade 3 [add 4 ?1] 5
== 17
val: 4
cascade 3 [add val ?1] 5
== 17
cascade2: func [
times
template1 start1
template2 start2
/local **temp**
] [
use [?1 ?2] [ ; to bind only ?1 and ?2 and to avoid variable capture
?1: start1
?2: start2
loop
times
bind
compose [**temp**: (template1) ?2: (template2) ?1: **temp**]
'?1
?1
]
]
cascade2 5 [?1 * ?2] 1 [?2 + 1] 1
== 120
cascade2 5 [?1 + ?2] 1 [?1] 0
== 8
Here is a somewhat working cascade in Rebol. It won't work with op! datatype--i.e. +, *--but it will work with add and multiply. You may want to check out the higher order functions script to see some other examples. I haven't had time to write cascade2 yet
cascade: func [
times [integer!]
f [any-function!]
partial-args [series!]
last-arg
][
expression: copy reduce [last-arg]
repeat n times [
insert head expression partial-args
insert head expression get 'f
]
expression
]
With your examples:
probe cascade 3 :add [4] 5
print cascade 3 :add [4] 5
will result in:
[make action! [[
"Returns the addition of two values."
value1 [scalar! date!]
value2
]] 4 make action! [[
"Returns the addition of two values."
value1 [scalar! date!]
value2
]] 4 make action! [[
"Returns the addition of two values."
value1 [scalar! date!]
value2
]] 4 5]
17
and
probe cascade 8 :multiply [2] 1
print cascade 8 :multiply [2] 1
Will result in:
[make action! [[
"Returns the first value multiplied by the second."
value1 [scalar!]
value2 [scalar!]
]] 2 make action! [[
"Returns the first value multiplied by the second."
value1 [scalar!]
value2 [scalar!]
]] 2 make action! [[
"Returns the first value multiplied by the second."
value1 [scalar!]
value2 [scalar!]
]] 2 make action! [[
"Returns the first value multiplied by the second."
value1 [scalar!]
value2 [scalar!]
]] 2 make action! [[
"Returns the first value multiplied by the second."
value1 [scalar!]
value2 [scalar!]
]] 2 make action! [[
"Returns the first value multiplied by the second."
value1 [scalar!]
value2 [scalar!]
]] 2 make action! [[
"Returns the first value multiplied by the second."
value1 [scalar!]
value2 [scalar!]
]] 2 make action! [[
"Returns the first value multiplied by the second."
value1 [scalar!]
value2 [scalar!]
]] 2 1]
256

Bash: Iteration through directories acts weird

#!/bin/bash
for dir in "/home/$USER/sessions/out/*/"; do
size=$(stat -c %s $dir/log)
if [ 1 -ne 0 ]; then
echo $size.
fi
done
Started using bash today. I am trying to check the size of log file in all directories in home/sessions/out. If 1!=0 (always, as test), the file size of log should be printed. What I am getting is:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 57344001 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 966904.
I expect a period after each file size, I actually get one period. Are all file sizes appended to variable size before it reaches the if clause? I am confused.
The problem is that pathname expansion (expansion of '*') happens in the stat invocation, not in the loop declaration, because the path in the loop is quoted, but the path in stat invocation is not.
I.e. only one iteration of the loop is happening, with dir having value of /home/$USER/sessions/out/*/. That is then being expanded in stat invocation, supplying all the paths matching /home/$USER/sessions/out/*/log to stat.
The solution is to unquote the path in the loop declaration and quote it in the stat invocation.
Like this:
#!/bin/bash
for dir in /home/$USER/sessions/out/*/; do
size=$(stat -c %s "$dir/log")
if [ 1 -ne 0 ]; then
echo $size.
fi
done
Your whole program can be simplified down to just one line
stat -c %s /home/$USER/sessions/out/*/log
Not quite sure why you have a test condition of 1 != 0, which always evaluates to true in there. But, the following loop will do it for you:
for LOGFILE in `find $LOGPATH -type f -iname 'log'`
do
echo `stat -c %s $LOGFILE`'.'
done
I used the variable LOGPATH where you would use '/home/$USER/sessions/out.' If this is in a script file, it would be wise to use a variable like that, at the top of the script, so that it could easily be changed later if need be.
The '-type f' is there in case there are directories with the name log, that you might not want to parse. If that is not the case, then you can omit that.
I used the find command, because you have much more finely grained control over it. You can set search depth, file type, file size, and so on. To get the period at the end, simply single quote a period right at the end of the echo statement.
If you needed the filename, and then the size, just change the echo statement, like so:
echo "$LOGFILE" `stat -c %s $LOGFILE`'.'

Resources