Portable shell code to print alternative textResources for portable shell programmingCode duplication in a...
Taking out number of subarrays from an array which contains all the distinct elements of that array
How does turbine efficiency compare with internal combustion engines if all the turbine power is converted to mechanical energy?
How to setup a teletype to a unix shell
Is it insecure to have an ansible user with passwordless sudo?
Why didn’t Doctor Strange stay in the original winning timeline?
Dark side of an exoplanet - if it was earth-like would its surface light be detectable?
Sleeping solo in a double sleeping bag
Can pay be witheld for hours cleaning up after closing time?
To "hit home" in German
How can I use unicode in this condition?
Defense against attacks using dictionaries
Why don't politicians push for fossil fuel reduction by pointing out their scarcity?
Can you be convicted for being a murderer twice?
Can you feel passing through the sound barrier in an F-16?
How to avoid using System.String with Rfc2898DeriveBytes in C#
Potential new partner angry about first collaboration - how to answer email to close up this encounter in a graceful manner
Are thrust levers synchronized by default when pushed/pulled?
How to compare two different formulations of a problem?
Is it best to use a tie when using 8th notes off the beat?
Are required indicators necessary for radio buttons?
How do you call it when two celestial bodies come as close to each other as they will in their current orbits?
Is a butterfly one or two animals?
What is the difference between a premise and an assumption in logic?
Most practical knots for hitching a line to an object while keeping the bitter end as tight as possible, without sag?
Portable shell code to print alternative text
Resources for portable shell programmingCode duplication in a shell scriptEnvironment variable for command called via niceHow portable is this shell assignment?I have a multi-line string that I need to iterate over, but I'm not using bash. Advice?Is “Arithmetic Expansion” the expected action on vars inside `[[` tests?bash while/read loop behaves differently in a mips/musl/busybox based VMshell code change variable on call of codeInteger with leading zeros (portable)?How to get du -ksh working without a carriage return in shell-scripting?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Playing on a question for codegolf.se I am out of ideas. Is there some other approach (reasonably short, of course) that I haven't tried?
Given i
contains either heads
or tails
, correspondingly output 1
or -1
.
Alternatives tried:
<<<$[#i%7-5] # 12 bytes but only zsh.
Try It Online!
<<<$[30#$i%7-1] # 15 bytes but only zsh.
Try It Online!
echo $[30#$i%7-1] # 17 bytes but only in bash, zsh.
Try It Online!
echo $((30#$i%7-1)) # 19 bytes but only bash,ksh,zsh.
Try It Online!
he=2;echo $[${i%a*}-1] # 22 bytes But only in bash,zsh.
Try It Online!
a=${i%h*};echo ${a:+-}1 # 23 bytes. best portable code
Try It Online!
he=2;echo $((${i%a*}-1)) # 24 bytes, portable.
Try It Online!
(IFS=h;set $i;echo ${1:+-}1) # 28 (subshell) or 26 (changing IFS).
Try It Online!
(IFS=h;set $i;echo $(($#*2-3))) # 31 bytes, portable.
Try It Online!
Note: Using dash
as a reasonable simile of a portable shell tester.
shell-script shell
add a comment |
Playing on a question for codegolf.se I am out of ideas. Is there some other approach (reasonably short, of course) that I haven't tried?
Given i
contains either heads
or tails
, correspondingly output 1
or -1
.
Alternatives tried:
<<<$[#i%7-5] # 12 bytes but only zsh.
Try It Online!
<<<$[30#$i%7-1] # 15 bytes but only zsh.
Try It Online!
echo $[30#$i%7-1] # 17 bytes but only in bash, zsh.
Try It Online!
echo $((30#$i%7-1)) # 19 bytes but only bash,ksh,zsh.
Try It Online!
he=2;echo $[${i%a*}-1] # 22 bytes But only in bash,zsh.
Try It Online!
a=${i%h*};echo ${a:+-}1 # 23 bytes. best portable code
Try It Online!
he=2;echo $((${i%a*}-1)) # 24 bytes, portable.
Try It Online!
(IFS=h;set $i;echo ${1:+-}1) # 28 (subshell) or 26 (changing IFS).
Try It Online!
(IFS=h;set $i;echo $(($#*2-3))) # 31 bytes, portable.
Try It Online!
Note: Using dash
as a reasonable simile of a portable shell tester.
shell-script shell
There'sexpr $i : he - 1
– Stéphane Chazelas
yesterday
@StéphaneChazelas Good one! Thanks.
– Isaac
yesterday
add a comment |
Playing on a question for codegolf.se I am out of ideas. Is there some other approach (reasonably short, of course) that I haven't tried?
Given i
contains either heads
or tails
, correspondingly output 1
or -1
.
Alternatives tried:
<<<$[#i%7-5] # 12 bytes but only zsh.
Try It Online!
<<<$[30#$i%7-1] # 15 bytes but only zsh.
Try It Online!
echo $[30#$i%7-1] # 17 bytes but only in bash, zsh.
Try It Online!
echo $((30#$i%7-1)) # 19 bytes but only bash,ksh,zsh.
Try It Online!
he=2;echo $[${i%a*}-1] # 22 bytes But only in bash,zsh.
Try It Online!
a=${i%h*};echo ${a:+-}1 # 23 bytes. best portable code
Try It Online!
he=2;echo $((${i%a*}-1)) # 24 bytes, portable.
Try It Online!
(IFS=h;set $i;echo ${1:+-}1) # 28 (subshell) or 26 (changing IFS).
Try It Online!
(IFS=h;set $i;echo $(($#*2-3))) # 31 bytes, portable.
Try It Online!
Note: Using dash
as a reasonable simile of a portable shell tester.
shell-script shell
Playing on a question for codegolf.se I am out of ideas. Is there some other approach (reasonably short, of course) that I haven't tried?
Given i
contains either heads
or tails
, correspondingly output 1
or -1
.
Alternatives tried:
<<<$[#i%7-5] # 12 bytes but only zsh.
Try It Online!
<<<$[30#$i%7-1] # 15 bytes but only zsh.
Try It Online!
echo $[30#$i%7-1] # 17 bytes but only in bash, zsh.
Try It Online!
echo $((30#$i%7-1)) # 19 bytes but only bash,ksh,zsh.
Try It Online!
he=2;echo $[${i%a*}-1] # 22 bytes But only in bash,zsh.
Try It Online!
a=${i%h*};echo ${a:+-}1 # 23 bytes. best portable code
Try It Online!
he=2;echo $((${i%a*}-1)) # 24 bytes, portable.
Try It Online!
(IFS=h;set $i;echo ${1:+-}1) # 28 (subshell) or 26 (changing IFS).
Try It Online!
(IFS=h;set $i;echo $(($#*2-3))) # 31 bytes, portable.
Try It Online!
Note: Using dash
as a reasonable simile of a portable shell tester.
shell-script shell
shell-script shell
edited 2 days ago
muru
43.8k5 gold badges110 silver badges181 bronze badges
43.8k5 gold badges110 silver badges181 bronze badges
asked 2 days ago
IsaacIsaac
14.4k1 gold badge23 silver badges61 bronze badges
14.4k1 gold badge23 silver badges61 bronze badges
There'sexpr $i : he - 1
– Stéphane Chazelas
yesterday
@StéphaneChazelas Good one! Thanks.
– Isaac
yesterday
add a comment |
There'sexpr $i : he - 1
– Stéphane Chazelas
yesterday
@StéphaneChazelas Good one! Thanks.
– Isaac
yesterday
There's
expr $i : he - 1
– Stéphane Chazelas
yesterday
There's
expr $i : he - 1
– Stéphane Chazelas
yesterday
@StéphaneChazelas Good one! Thanks.
– Isaac
yesterday
@StéphaneChazelas Good one! Thanks.
– Isaac
yesterday
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f536172%2fportable-shell-code-to-print-alternative-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f536172%2fportable-shell-code-to-print-alternative-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There's
expr $i : he - 1
– Stéphane Chazelas
yesterday
@StéphaneChazelas Good one! Thanks.
– Isaac
yesterday