For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://...
Cecil Westerhof <Cecil@decebal.nl> writes:
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://...
Why post a link? We discuss shell programming here, not there! Post
the content here.
Posting links and no content is what spammers do and I'm sure you don't
want to be taken for a spammer.
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://www.linkedin.com/pulse/script-add-swap-file-cecil-westerhof
You can get the script here:
https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/addSwap
File.sh
In article <87mtc45okr.fsf@bsb.me.uk>,
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://...
Why post a link? We discuss shell programming here, not there! Post
the content here.
Posting links and no content is what spammers do and I'm sure you don't >>want to be taken for a spammer.
Let me first say that I 100% agree with you in principle. That in the
model of classic Usenet, sure, OP could have just posted his script in the item. However, let me play devil's advocate for the rest of this post:
1) We live in a "clicks equals money" world, and it is quite possible that
OP was looking to make a little cash from having you follow his links.
Given that we live in a world where the elites are crushing the middle
class, and everybody is encouraged to have a "side hustle" or two, I can't say this is necessarily a bad thing (on OP's part).
2) I'm not sure that OP is interested in the usual "human compiler"
treatment that we usually deal in here on Usenet. That is, where eager
folks like you and me go through his script line by line, like a compiler would, and make derogatory comments where we can. Instead, I think he was just putting it out there as something you may (or may not) enjoy reading.
I really didn't get the impression that he wanted or was seeking any human compilers.
Note, BTW, that I did go and read his post (on linked in) and if I *WAS* so inclined, I could make some (in fact, quite a few) human compiler comments, but I do not intend on doing so.
In article <87mtc45okr.fsf@bsb.me.uk>,
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
For people who know Bash, but like to learn write (better) scripts
I wrote an article about a script I wrote to create and add a
swapfile: https://...
Why post a link? We discuss shell programming here, not there! Post
the content here.
Posting links and no content is what spammers do and I'm sure you
don't want to be taken for a spammer.
Let me first say that I 100% agree with you in principle. That in the
model of classic Usenet, sure, OP could have just posted his script
in the item.
However, let me play devil's advocate for the rest of this post:
1) We live in a "clicks equals money" world, and it is quite possible
that OP was looking to make a little cash from having you follow his
links. Given that we live in a world where the elites are crushing
the middle class, and everybody is encouraged to have a "side hustle"
or two, I can't say this is necessarily a bad thing (on OP's part).
2) I'm not sure that OP is interested in the usual "human compiler"
treatment that we usually deal in here on Usenet. That is, where
eager folks like you and me go through his script line by line, like
a compiler would, and make derogatory comments where we can.
Instead, I think he was just putting it out there as something you
may (or may not) enjoy reading. I really didn't get the impression
that he wanted or was seeking any human compilers.
Note, BTW, that I did go and read his post (on linked in) and if I
*WAS* so inclined, I could make some (in fact, quite a few) human
compiler comments, but I do not intend on doing so.
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://www.linkedin.com/pulse/script-add-swap-file-cecil-westerhof
You can get the script here:
https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/addSwapFile.sh
I have interacted with Cecil in person off of Usenet several years ago regarding a presentation he was creating on account of systemd — he
himself may not remember that anymore, but I happen to have an eidetic memory.
Cecil is not the kind of person you are describing above. He is
someone who's eager to contribute to the FLOSS community, he
absolutely loves scripting in GNU bash, and he takes pride in his work.
From my prior interactions with Cecil, I know that he always welcomes
all constructive commentary. But he wasn't fishing for it, and
knowing
him, his code is probably sufficiently good already, even if perhaps
not perfect.
Cecil Westerhof <Cecil@decebal.nl> writes:
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://www.linkedin.com/pulse/script-add-swap-file-cecil-westerhof
You can get the script here:
https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/addSwapFile.sh
The script my post explains.
#!/usr/bin/env bash
set -o errexit
set -o nounset
function getFreeGB {
df -BG $(dirname $1) | \
tail -n 1 | \
awk '{print substr($4, 1, length($4) - 1)}'
}
function giveError {
echo $1
exit 1
}
declare -r _scriptName=$(basename ${0})
if [[ $(id --user) -ne 0 ]] ; then
giveError "${_scriptName} should be run as root"
fi
if [[ $# -ne 2 ]] ; then
giveError "ERROR: ${_scriptName} SWAPFILE GB"
fi
declare -r _swapFile=$1
declare -ir _gb=$2
if [[ -e ${_swapFile} ]] ; then
giveError "${_swapFile} already exists"
fi
declare -ir _gbFree=$(getFreeGB ${_swapFile})
# Make sure swapfile is less as a quarter of free space
if [[ ${_gbFree} -le $(($_gb * 4)) ]] ; then
giveError "Not enough space for swap file ($_gb, ${_gbFree})"
fi
echo Current Swap:
swapon
echo
date "+%T: Creating ${_swapFile}"
dd if=/dev/zero of=${_swapFile} bs=1024 count=$(($_gb * 1024 ** 2))
echo
chmod 600 ${_swapFile}
mkswap ${_swapFile}
swapon ${_swapFile}
echo New Swap:
swapon
On 16.08.2022 19:37, Cecil Westerhof wrote:
Since you want only the last line of the df output you can let awk do
that job; at the end of processing the last processed input line is
still available in the END section, a simple change
df -BG $(dirname $1) |
awk 'END {print substr($4, 1, length($4) - 1)}'
On 8/16/2022 12:37 PM, Cecil Westerhof wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://www.linkedin.com/pulse/script-add-swap-file-cecil-westerhof >>>
You can get the script here:
https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/addSwapFile.sh
The script my post explains.
You should copy/paste your script into http://shellcheck.net and fix the >issues it tells you about (e.g. the missing quotes in several places).
I created a bash group on LinkedIn and there was a question about how
to learn to program in bash. So I wrote a script and an article
explaining the script with hopefully interesting tips for people how
to create their own scripts.
I just thought/hoped it could be interesting for people using this
newsgroup.
Cecil Westerhof <Cecil@decebal.nl> writes:
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://www.linkedin.com/pulse/script-add-swap-file-cecil-westerhof
You can get the script here:
https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/addSwapFile.sh
The script my post explains.
#!/usr/bin/env bash
set -o errexit
set -o nounset
function getFreeGB {
df -BG $(dirname $1) | \
tail -n 1 | \
awk '{print substr($4, 1, length($4) - 1)}'
}
function giveError {
echo $1
exit 1
}
declare -r _scriptName=$(basename ${0})
if [[ $(id --user) -ne 0 ]] ; then
giveError "${_scriptName} should be run as root"
fi
if [[ $# -ne 2 ]] ; then
giveError "ERROR: ${_scriptName} SWAPFILE GB"
fi
declare -r _swapFile=$1
declare -ir _gb=$2
if [[ -e ${_swapFile} ]] ; then
giveError "${_swapFile} already exists"
fi
declare -ir _gbFree=$(getFreeGB ${_swapFile})
# Make sure swapfile is less as a quarter of free space
if [[ ${_gbFree} -le $(($_gb * 4)) ]] ; then
giveError "Not enough space for swap file ($_gb, ${_gbFree})"
fi
echo Current Swap:
swapon
echo
date "+%T: Creating ${_swapFile}"
dd if=/dev/zero of=${_swapFile} bs=1024 count=$(($_gb * 1024 ** 2))
echo
chmod 600 ${_swapFile}
mkswap ${_swapFile}
swapon ${_swapFile}
echo New Swap:
swapon
You're using `{...}` unnecessarily everywhere, I think maybe you're mistaking what curly brackets do (just separate a variable from
concatenated text) vs what double quotes do (protect your variable
from globbing, word splitting, and filename expansion).
The use of `function foo {` is non-portable, use `foo() {` instead.
On 17.08.2022 at 09:34, Ed Morton scribbled:
You're using `{...}` unnecessarily everywhere, I think maybe you're
mistaking what curly brackets do (just separate a variable from
concatenated text) vs what double quotes do (protect your variable
from globbing, word splitting, and filename expansion).
There's nothing wrong with being consistent in using accolades for all variable names.
On 8/17/2022 5:32 AM, Janis Papanagnou wrote:
On 16.08.2022 19:37, Cecil Westerhof wrote:
<snip>
Since you want only the last line of the df output you can let awk do
that job; at the end of processing the last processed input line is
still available in the END section, a simple change
df -BG $(dirname $1) |
awk 'END {print substr($4, 1, length($4) - 1)}'
At the end of processing the last processed input line is not guaranteed
to still available in the END section.
The value of $4 or any other field or $0 is undefined (by POSIX) in the
END section. In some awk variants it will be the $4 from the last record
read as you want, but in others it'll be null, and in others still it
could be anything else. To do what you show above portably in all awks
would be:
awk '{p=$4} END{print substr(p, 1, length(p) - 1)}'
On 16.08.2022 19:37, Cecil Westerhof wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://www.linkedin.com/pulse/script-add-swap-file-cecil-westerhof
You can get the script here:
https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/addSwapFile.sh
The script my post explains.
Since you spread (thus multiply) knowledge about scripting I'd like to
give a few comments...
function getFreeGB {
df -BG $(dirname $1) | \
tail -n 1 | \
awk '{print substr($4, 1, length($4) - 1)}'
}
In scripting the backslashes as a line continuation are a bad concept
since you don't see whether there's whitespace characters (other than
the newline character) there. Specifically a final pipe symbol doesn't
need any explicit textual line continuation so it can be anyway just
omitted.
Since you want only the last line of the df output you can let awk do
that job; at the end of processing the last processed input line is
still available in the END section, a simple change
df -BG $(dirname $1) |
awk 'END {print substr($4, 1, length($4) - 1)}'
and since you want to strip just the numerical part from the field $4
you can simplify that substr/langth by doing numeric "type casting"
df -BG $(dirname $1) |
awk 'END {print 0+$4}'
Below I am wondering why you are not consistently using braces when
expanding variables; we see ${0} and $2, and we see ${_gbFree} and
$_gb. I think this is confusing for folks that are learning shell -
as I understood that this was the (or one) intention of your post.
Whatever you prefer, but mixing is not a good thing as a paragon.
Since you're using a modern shell like bash (and obviously don't
restrict to POSIX)
I wonder why you don't use arithmetic expressions. Instead of
if [[ ${_gbFree} -le $(($_gb * 4)) ]]
there's the much clearer
if (( _gbFree <= _gb * 4 ))
Finally, and especially if a variable is named "File", I'd quote the
variable expansions ("${_swapFile}"), as a paragon for the learning
folks, but also as an actual safety measure (since it's initialized
by the caller through $1, which can contain white-space characters).
Note: I haven't read the article behind your posted link, and I also
haven't inspected every line of your code; my comments are just from
a few quite obvious observations in the posted shell source code.
awk '{p=$4} END{print substr(p, 1, length(p) - 1)}'
For me it works, but I should implement something like this then.
On 8/16/2022 12:30 PM, Cecil Westerhof wrote:
<snip>
I created a bash group on LinkedIn and there was a question about how
to learn to program in bash. So I wrote a script and an article
explaining the script with hopefully interesting tips for people how
to create their own scripts.
I just thought/hoped it could be interesting for people using this
newsgroup.
Then I wish you had posted your script and article here and pointed
people from LinkedIn here instead of doing it the other way around.
Ed Morton <mortonspam@gmail.com> writes:
On 8/16/2022 12:30 PM, Cecil Westerhof wrote:
<snip>
I created a bash group on LinkedIn and there was a question about how
to learn to program in bash. So I wrote a script and an article
explaining the script with hopefully interesting tips for people how
to create their own scripts.
I just thought/hoped it could be interesting for people using this
newsgroup.
Then I wish you had posted your script and article here and pointed
people from LinkedIn here instead of doing it the other way around.
I do not think that will work. I love usenet, but we (users of usenet)
are a dying race. Most people do not even know it exists.
On 8/16/2022 12:37 PM, Cecil Westerhof wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
For people who know Bash, but like to learn write (better) scripts IThe script my post explains.
wrote an article about a script I wrote to create and add a swapfile:
https://www.linkedin.com/pulse/script-add-swap-file-cecil-westerhof >>>
You can get the script here:
https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/addSwapFile.sh
You should copy/paste your script into http://shellcheck.net and fix the issues it tells you about (e.g. the missing quotes in several places).
A few additional things I noticed:
You're using `{...}` unnecessarily everywhere, I think maybe you're
mistaking what curly brackets do (just separate a variable from
concatenated text) vs what double quotes do (protect your variable from globbing, word splitting, and filename expansion).
The use of `function foo {` is non-portable, use `foo() {` instead.
When printing error messages (e.g. inside "giveError()") add `>&2` so
the message goes to stderr instead of stdout.
On 8/17/2022 10:48 AM, Cecil Westerhof wrote:
Ed Morton <mortonspam@gmail.com> writes:
On 8/16/2022 12:30 PM, Cecil Westerhof wrote:I do not think that will work. I love usenet, but we (users of usenet)
<snip>
I created a bash group on LinkedIn and there was a question about how
to learn to program in bash. So I wrote a script and an article
explaining the script with hopefully interesting tips for people how
to create their own scripts.
I just thought/hoped it could be interesting for people using this
newsgroup.
Then I wish you had posted your script and article here and pointed
people from LinkedIn here instead of doing it the other way around.
are a dying race. Most people do not even know it exists.
Usenet was just one example of an existing forum, if you don't like
usenet
Ed Morton <mortonspam@gmail.com> writes:<snip>
The use of `function foo {` is non-portable, use `foo() {` instead.
It is a bash script, so it is in my opinion the preferred way.
In a way I like to confirm to POSIX. But not rigorously.
Ed Morton <mortonspam@gmail.com> writes:
On 8/17/2022 10:48 AM, Cecil Westerhof wrote:
Ed Morton <mortonspam@gmail.com> writes:
On 8/16/2022 12:30 PM, Cecil Westerhof wrote:I do not think that will work. I love usenet, but we (users of usenet)
<snip>
I created a bash group on LinkedIn and there was a question about how >>>>> to learn to program in bash. So I wrote a script and an article
explaining the script with hopefully interesting tips for people how >>>>> to create their own scripts.
I just thought/hoped it could be interesting for people using this
newsgroup.
Then I wish you had posted your script and article here and pointed
people from LinkedIn here instead of doing it the other way around.
are a dying race. Most people do not even know it exists.
Usenet was just one example of an existing forum, if you don't like
usenet
What in the sentence 'I love usenet' did you make think that I do not
like it?
I agree with that, and I myself always write my scripts in the most
portable way, but the OP has very clearly indicated that it is a bash
script, and that it's specifically intended for use in GNU/Linux, where
GNU bash is still the main shell.
In article <871qteamv5.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:
...
awk '{p=$4} END{print substr(p, 1, length(p) - 1)}'
For me it works, but I should implement something like this then.
Or you could just use either mawk or gawk, which are known to work.
I always recommend against using plain old "awk" in a script, b/c you never know what you are going to get.
Until fairly recently, using plain "awk" under Solaris got you a very old, essentially non-functional version of AWK.
Usenet was just one example of an existing forum, if you don't like
usenet
What in the sentence 'I love usenet' did you make think that I do not
like it?
If you want your code to be compatible with ksh then, depending on
what
If you don't care about any of that then why not just make it the most portable, `name ()`, especially since you're providing it as an example
for others to emulate and they might be trying to implement similar
functions in other shells.
On 8/17/2022 11:39 AM, Cecil Westerhof wrote:
Ed Morton <mortonspam@gmail.com> writes:
On 8/17/2022 10:48 AM, Cecil Westerhof wrote:
Ed Morton <mortonspam@gmail.com> writes:
On 8/16/2022 12:30 PM, Cecil Westerhof wrote:
<snip>
I created a bash group on LinkedIn and there was a question about how >>>>>> to learn to program in bash. So I wrote a script and an article
What in the sentence 'I love usenet' did you make think that I do notI do not think that will work. I love usenet, but we (users of usenet) >>>> are a dying race. Most people do not even know it exists.explaining the script with hopefully interesting tips for people how >>>>>> to create their own scripts.
I just thought/hoped it could be interesting for people using this >>>>>> newsgroup.
Then I wish you had posted your script and article here and pointed
people from LinkedIn here instead of doing it the other way around.
Usenet was just one example of an existing forum, if you don't like
usenet
like it?
I mean "like" in the context of where to post your script.
Ed Morton <mortonspam@gmail.com> writes:
On 8/17/2022 11:39 AM, Cecil Westerhof wrote:
Ed Morton <mortonspam@gmail.com> writes:
On 8/17/2022 10:48 AM, Cecil Westerhof wrote:
Ed Morton <mortonspam@gmail.com> writes:
On 8/16/2022 12:30 PM, Cecil Westerhof wrote:
<snip>
I created a bash group on LinkedIn and there was a question about how >>>>>>> to learn to program in bash. So I wrote a script and an article
And this is why I posted it there and not here.
What in the sentence 'I love usenet' did you make think that I do notexplaining the script with hopefully interesting tips for people how >>>>>>> to create their own scripts.
I just thought/hoped it could be interesting for people using this >>>>>>> newsgroup.
Then I wish you had posted your script and article here and pointed >>>>>> people from LinkedIn here instead of doing it the other way around. >>>>> I do not think that will work. I love usenet, but we (users of usenet) >>>>> are a dying race. Most people do not even know it exists.
Usenet was just one example of an existing forum, if you don't like
usenet
like it?
I mean "like" in the context of where to post your script.
I like to post it here, but it is not only about like, it is also
about being effective. It is not opportune to answer a question in the LinkedIn bash group on usenet.
Ed Morton <mortonspam@gmail.com> writes:
If you want your code to be compatible with ksh then, depending on
what
No, I do not want that.
If you don't care about any of that then why not just make it the most
portable, `name ()`, especially since you're providing it as an example
for others to emulate and they might be trying to implement similar
functions in other shells.
You would have preferred that everyone used BASICODE?
https://en.wikipedia.org/wiki/BASICODE
On 8/17/2022 12:29 PM, Cecil Westerhof wrote:
Ed Morton <mortonspam@gmail.com> writes:
On 8/17/2022 11:39 AM, Cecil Westerhof wrote:
Ed Morton <mortonspam@gmail.com> writes:
On 8/17/2022 10:48 AM, Cecil Westerhof wrote:
Ed Morton <mortonspam@gmail.com> writes:
On 8/16/2022 12:30 PM, Cecil Westerhof wrote:
<snip>
I created a bash group on LinkedIn and there was a question about how >>>>>>>> to learn to program in bash. So I wrote a script and an article
And this is why I posted it there and not here.
What in the sentence 'I love usenet' did you make think that I do notexplaining the script with hopefully interesting tips for people how >>>>>>>> to create their own scripts.
I just thought/hoped it could be interesting for people using this >>>>>>>> newsgroup.
Then I wish you had posted your script and article here and pointed >>>>>>> people from LinkedIn here instead of doing it the other way around. >>>>>> I do not think that will work. I love usenet, but we (users of usenet) >>>>>> are a dying race. Most people do not even know it exists.
Usenet was just one example of an existing forum, if you don't like
usenet
like it?
I mean "like" in the context of where to post your script.
I like to post it here, but it is not only about like, it is also
about being effective. It is not opportune to answer a question in the
LinkedIn bash group on usenet.
I didn't suggest doing that. I suggested posting your script on some
commonly used existing forum where it could help many people and
answering the question where it was asked but point them to the script
on that common forum rather than doing the opposite of posting your
script on LinkedIn and then posting messages on other forums directing
people to LinkedIn.
Ed.
On 8/17/2022 5:32 AM, Janis Papanagnou wrote:
On 16.08.2022 19:37, Cecil Westerhof wrote:
<snip>
Since you want only the last line of the df output you can let awk do
that job; at the end of processing the last processed input line is
still available in the END section, a simple change
df -BG $(dirname $1) |
awk 'END {print substr($4, 1, length($4) - 1)}'
At the end of processing the last processed input line is not guaranteed
to still available in the END section.
The value of $4 or any other field or $0 is undefined (by POSIX)
in the
END section. In some awk variants it will be the $4 from the last record
read as you want, but in others it'll be null, and in others still it
could be anything else. To do what you show above portably in all awks
would be:
awk '{p=$4} END{print substr(p, 1, length(p) - 1)}'
Regards,
Ed.
Aragorn <thorongil@telenet.be> writes:
I agree with that, and I myself always write my scripts in the most
portable way, but the OP has very clearly indicated that it is a bash
script, and that it's specifically intended for use in GNU/Linux, where
GNU bash is still the main shell.
I have heard of people that used my bash library on a non Linux
system, but it is a very small minority. I myself work only with
Linux.
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://www.linkedin.com/pulse/script-add-swap-file-cecil-westerhof
You can get the script here:
https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/addSwapFile.sh
Janis Papanagnou <janis_papanagnou@hotmail.com> writes:
On 16.08.2022 19:37, Cecil Westerhof wrote:
Cecil Westerhof <Cecil@decebal.nl> writes:
function getFreeGB {
df -BG $(dirname $1) | \
tail -n 1 | \
awk '{print substr($4, 1, length($4) - 1)}'
}
In scripting the backslashes as a line continuation are a bad concept
since you don't see whether there's whitespace characters (other than
the newline character) there. Specifically a final pipe symbol doesn't
need any explicit textual line continuation so it can be anyway just
omitted.
I know that you have to be careful that the backslash is the last
character on the line, but I find it important that lines (if
possible) are less as 80 characters.
I did not know that the final pipe symbol does not need an explicit
context. That is going to make my live easier.
Below I am wondering why you are not consistently using braces when
expanding variables; we see ${0} and $2, and we see ${_gbFree} and
$_gb. I think this is confusing for folks that are learning shell -
as I understood that this was the (or one) intention of your post.
Whatever you prefer, but mixing is not a good thing as a paragon.
Oops, that I used ${0} was certainly wrong. :'-(
Let me explain. In the past people where really annoyed by me using
braces. "It is not necessary." But there are some cases where they are important. That is why I decided that when the length of a name is not
more as 4 characters long I will not use braces and otherwise I will.
But I should explain that.
Since you're using a modern shell like bash (and obviously don't
restrict to POSIX)
I am a bit on the fence with that. In a way I like to confirm to
POSIX. But not rigorously. For example I use long options, but
mentions what the corresponding short ones are. Handy for people that
have a POSIX compliant awk.
On 17.08.2022 17:40, Cecil Westerhof wrote:
Oops, that I used ${0} was certainly wrong. :'-(
Erm, ${0} is not "wrong", I just meant that it's inconsistently used.
Let me explain. In the past people where really annoyed by me using
braces. "It is not necessary." But there are some cases where they are
important. That is why I decided that when the length of a name is not
more as 4 characters long I will not use braces and otherwise I will.
But I should explain that.
In my scripts I use generally braces, to be consistent, and to make
it work in any of the cases where it's necessary. Examples for that; composition of variable name with alphanumeric suffix: ${var}_suffix,
or with positional parameters for numbers greater than 9: ${12}, or
to be syntactically consistent with the complex string operations,
e.g. ${var/x/y}, ${#str}, ${a[@]}, etc.
without loss. On the other hand - note I'm using ksh - I often use
the ksh extensions if they are also supported by bash and zsh, and
I use the major extensions of ksh that are not available in bash
if unavoidable (e.g. built-in floating point arithmetic [without
falling back to external processes], or complex things like the
type system, discipline functions, and similar).
On 17.08.2022 18:50, Cecil Westerhof wrote:
Aragorn <thorongil@telenet.be> writes:
I agree with that, and I myself always write my scripts in the most
portable way, but the OP has very clearly indicated that it is a bash
script, and that it's specifically intended for use in GNU/Linux, where
GNU bash is still the main shell.
I have heard of people that used my bash library on a non Linux
system, but it is a very small minority. I myself work only with
Linux.
You have specified an interpreter as first line #!/usr/bin/env bash
so I assume it's an executable - or at least that's a documentation
for users to not let it run under different shells. - That's fine.
On 2022-08-16, Cecil Westerhof <Cecil@decebal.nl> wrote:
For people who know Bash, but like to learn write (better) scripts I
wrote an article about a script I wrote to create and add a swapfile:
https://www.linkedin.com/pulse/script-add-swap-file-cecil-westerhof
You can get the script here:
https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/addSwapFile.sh
A script like this is a good idea.
The last time I needed to make a swap file on Linux was at least fifteen years ago. I can spew the commands to do it in my sleep: dd to create
the file, then mkswap and swapon.
Yet, I never "chmod 0600" on the file; and root's umask is often 022,
leaving files readable to others.
Well, maybe I can change my ways again. But honestly I find ${0}
looking a bit weird. That is why I find it so strange I used it.
On 17.08.2022 23:37, Cecil Westerhof wrote:
Well, maybe I can change my ways again. But honestly I find ${0}
looking a bit weird. That is why I find it so strange I used it.
Frankly, I don't use ${1} etc., I use $1. [...]
Until fairly recently, using plain "awk" under Solaris got you a very old, essentially non-functional version of AWK.
awk '{p=$4} END{print substr(p, 1, length(p) - 1)}'
For me it works, but I should implement something like this then.
Also I wonder if a pagefile actually needs to prewritten out like that
with dd or if creating a sparse file with truncate would be enough.
As they say: things you do seldom you should automate.
Cecil Westerhof <Cecil@decebal.nl> wrote:
As they say: things you do seldom you should automate.
I have a different viewpoint. Things you do often should be
automated. Things you do seldom should be documented.
Writing a wiki page to document a procedure takes much less time
than writing/testing/bugfixing automation for the procedure.
Both
repay the time spent creating them in small increments each time
the procedure is repeated.
The long time spent automating the steps will take 5 or 6 repeats
(or more) to repay.
A seldom-used procedure may never reach that
point, but an often-used procedure will. The short time documenting
the steps will take only 1 or 2 repeats to repay - far more achievable
by a seldom-used procedure.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 498 |
Nodes: | 16 (0 / 16) |
Uptime: | 72:24:18 |
Calls: | 9,819 |
Calls today: | 7 |
Files: | 13,757 |
Messages: | 6,189,681 |