SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello to all friendly Slackers, whether you remember me, or not. I'm writing a new project and had already asked elsewhere for testers/bug-hunters.
It IS a pretty crazy idea and implementation, so of course I got the Why this?, Have you tried That? Why in That language?, etc. And then they started looking at the code that typically looks something like this:
Code:
# tsst - numeric comparison of decimal numbers using shell 'test' syntax
# depends on: 'cmp3w'
tsst() { case $1 in ''|-h|-s*) tsst_help >&2 ; return 1 ;;esac
tstret=$( cmp3w $1 $3 )
case $2 in
'-lt') [ "$tstret" = '<' ] ; return $? ;;
'-le') case $tstret in '<'|'=') return 0 ;; *) return 1 ;;esac ;;
'-eq') [ "$tstret" = '=' ] ; return $? ;;
'-ge') case $tstret in '>'|'=') return 0 ;; *) return 1 ;;esac ;;
'-gt') [ "$tstret" = '>' ] ; return $? ;;
'-ne') case $tstret in '=') return 1 ;; *) return 0 ;;esac ;;
esac
} ## tsst
Usually it has at least Bread-crumb comments in functions, but not here.
So, Jan K., you liked example #2 best. That's a bash 'declare - tsst' which shows the internal storage format of a function in bash. I wondered, once, if saving them pre-formatted would make them run faster, but of course it did not.
Thanks, kjh!
Hi, gnashley (I do remember you ). #2 looks best to me, but #1 wasn't bad either, only the "case $1..." line looked a bit crowded. I'm a C/Rust person, so I am more inclined to like the indentation practices in those languages. As the others said, it is your project, and I think you should use what is most comfortable for you. But if you expect others to contribute to or maintain the code in the long run, it might be a good idea to choose a style that makes the others feel comfortable, too.
The condition "siblings" are all under the same glance. Additionally, you don't have to look far to compare the outcomes.
My colleagues enjoy getting into these exercises and some of us have many more years worth of opinion to throw around. Personally coming from a good chunk of COBOL work, I really appreciate minimizing eye travel. I feel like it's easier on my dusty short-term memory.
Absolutely remember, and I still use src2pkg, albeit more rarely now.
That said, I go with Block #1
I am not a code writer, but I like the linear relationship of the code. Don't have my eyes dancing all over the place. But still, do what YOU like!
Jan K. The project was on github til days ago, but I removed the repo in order to start over.
cowpoke -with a handle like that, you probably come from a lonely place near where I grew up...
For those who remember src2pkg, now in advanced bit-rot, if you ever had a peek at the code(of course you did), you'll find my recent work refreshing.
teckk, ummm you should have remembered the code in example #1, since you happen to be 'guy #1' -you said this:
Quote:
1000 lines of bash all squished together
Apparently you found the shebang too confusing, since it says '#!/bin/sh' and not '#!/bin/bash' and I had explicitly said that it was posix-shell language. Do you have another mode you can run in?
Back to the subject -I admit that I've already decided how to format my code, so that what the reader might want to understand, is commented and just roomy enough to expose the meat of the function. And actually, since it is a script, all unneeded characters do cost something to process -even white space and quote-marks. I just wanted to test the waters a bit here before announcing anything. Recently I've noticed a wanna-be Slacker around here terrorizing even those whose projects are in the standard Slackware software selection. He hasn't shown up here yet, but I expect him as soon as I create a thread with 'testers wanted' in the title. And I was also wondering if we'd get to post #5 without someone bringing up the 'Icon Issues' in my profile. This is Day #2 and those things have not happened, so the future looks bright...
I'll start a new thread within a couple of days, and simply attach the script which currently is:
As mentioned, white space, quote marks -in a script every character slows it down. Once I tried a style-suggestion from shellcheck about double-quoting some vars -a construct I was using throughout- anyway, the startup latency went up by 5X -just for about 1K worth of "".
Since Irony has already visited this thread: After the quoted response above, from Guy#1, I thought Oh boy! here we go. And then the discussion fell into arguments about what "posix-compliant" consists of, and suggestions of cleaner/faster(sic) alternate usages -everyone of them a bashism, mind you this on the LQ Programming forum.
Anyway, I decided to ask for the same help on another forum where -let's put it plainly- everyone is _always_ root.
I still had not written in any idiot/cat-checking clauses, but was having trouble duplicating rare errors when operating on numbers above 20-digits -the project is a calculator. So, I posed the same simple question -Can you get wrong answers from this with sane inputs. You'd think that people who are running as 'root' would have no interest in trying dumb stuff, but righ away I got this, from Guy#1b:
Code:
# why does 'div bla t/ blad' return this:
blad.0
I kindly pointed out that he could not prove that the answer was incorrect, after all, the 'stupid' program got these all correctly:
Code:
div 0 / bla = 0.0
div bla / 0 = division by zero error
div bla / 1 = bla
div bla / bla = 1.0
All perfectly correct, no? Then, unsurprisingly, said thread degenerates into such:
I have no idea what this does or how it does it, but this could be speeded 100X up by using:
'bashism' instead of 'my construct'. Two weeks later, Guy#1b is already looking to do a hostile-fork and take credit for my nearly two years of work -writing a calculator in pure-shell is no piece of cake -especially when you've been retired 5 years from coding, and haven't had a math class in 45 years.
And I had an even steeper learning curve because my goal was to be able to solve logistic-sigmoid equations. Basically I needed to solve: e^(-x.xxxx) and had never heard of Euler and had no idea what a logarithm was. By now, Said Calculator has an advanced module which can solve problems like this: 0.014^10000000 I give that specific example because ^10,000,000 was where the Casio online calculator would crash my browser.
So I asked the guys on the Programming forum if they could verify the answer. It took them awhile to find _any_ way to confirm that answer with one of perl, awk, python or whatever. They also tried Galculate and others. Meanwhile I had ratcheted-up the problem to ^10,000,000,000 -and beyond! They spent two days trying various bignum libraries for perl, awk and python, before finally finding something that worked -and the answer was correct. They were a bit hot under the collar by then, so I didn't point out that my 'pow' would only fail at:
X^10,000,000,000,000,000,000,000
and that a 3-line fix should let it do: X^trillion-digit-exponent -but who needs to go there and who would wait for such an answer?? My 'epow' solves X^10million in .3 seconds -if you ask for output in scientific notation. Using SigFig scaling would take a very long time -the calculation is done in the same time, but concatenating and printing all those leading zeros -18538500 exactly, would take quite a while.
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
I prefer the way as per the comment from teckk
>> White space with remarks is a good thing.
I used to write the coding:
New joinee can use it, when I resign current job.
Sample comments:
Code:
#!/bin/bash
#This script is a sample script for sharing my comments.
#Before defininig the function unalias any such name.
unalias RequiredFunction >/dev/null 2>&1
#Unsdefine RequiredFunction function which will over write the same function if this function used by other scripting.
unset -f RequiredFunction
RequiredFunction ()
{
if [ "-bash" = "$0" ]
then
echo "Executing this script inside current shell not allowed"
Ret=1
return $Ret
fi
if [ 0 -eq $# ]
then
echo handle usage exceptions
Ret=1
return $Ret
fi
# Handle parameter using if/fi or case/esac having related tabs.
echo "$@"
#I used to save $? to a variable and I prefer return $Ret
# Handle the value of $0 being -bash/bash/... based on OS to prevent current user using:
# . ./scriptname.sh
# => Executing script at current shell not allowed.
# use export variable if I need it using gawk.exe/awk using ENVIRON["myvariable"];
Ret=$?
return $Ret
}
# I feel to use % when placing the cursor at { or } to go to beginning/end of blocks.
# It can be used to use copy/paste blocks from current file to other file or inside the same file using markers.
RequiredFunction $@
Ret=$?
if [ 0 -eq $Ret ]
then
echo "Before_a unset Ret [$Ret]"
#Always unset the variables used inside the script
unset Ret
echo "After_a unset Ret [$Ret]"
# unset myvariable when scripting exiting any where.
else
echo "Before_b unset Ret [$Ret]"
#Always unset the variables used inside the script
unset Ret
echo "After_b unset Ret [$Ret]"
# unset myvariable when scripting exiting any where.
fi
I assumed the question was about readability of code. By someone else, and by the author, if you have left it for a month or two and then came back to it.
Both of these do the same thing. Which one is easier to understand.
Code:
log_time=120
counter() {
sleep_time=("$log_time")
while [ "$sleep_time" -gt 0 ]; do echo -ne "x to stop or Update in "$sleep_time" seconds \r"
read -t 1 -s -n 1 key
if [ "$key" = "x" ]; then sleep_time=("$log_time"); clear && "$0" && exit; fi
sleep_time=$(($sleep_time - 1));
done
}
Code:
#Retrieve interval for log
log_time=120
#Counter for next log display, or press x to restart script
counter() {
#Reset time for counter
sleep_time=("$log_time")
#Do until counter runs out
while [ "$sleep_time" -gt 0 ]; do
echo -ne "x to stop or Update in "$sleep_time" seconds \r"
#Look for key press every second
read -t 1 -s -n 1 key
#Look for x key to reset
if [ "$key" = "x" ]; then
sleep_time=("$log_time")
clear && "$0" && exit
fi
#Decrease counter by one
sleep_time=$(($sleep_time - 1))
done
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.