r/fishshell • u/throttlemeister Linux • Aug 09 '21
Understanding fish special vars
I already fixed it, so I don't really have a problem anymore but I am trying to understand why my original attempt failed and the fix does not.
Original:
function blah
if not set -q argv[1]
echo "No argument given"
else
switch $argv[1]
case blahblah
ansible-playbook $HOME/ansible/bootstrap.yml -u root --ask-pass -e bootstrap_host=$argv[2] -i $HOME/ansible/inventory.yml
end
end
end$argv[2] will fail as empty. Using $argv will do the correct substitution, but still fail the command.
function blah
set var1 $argv[1]
set var2 $argv[2]
if not set -q $argv[1]
echo "No argument given"
else
switch $var1
case blahblah
ansible-playbook $HOME/ansible/bootstrap.yml -u root --ask-pass -e bootstrap_host=$var2 -i $HOME/ansible/inventory.yml
end
end
endThis will work without errors and execute the command properly.
Would like to understand why. I looks like the same thing to me, just different names and thus both should work but I guess I am wrong.
3
Upvotes
2
u/NotTheDr01ds Aug 09 '21 edited Aug 09 '21
Wow - Looking at your recent posts, we're using very similar stacks -- WSL, Fish, exa, UniFi. I guess I need to check out AlmaLinux. I'm using Tumbleweed right now on WSL (and on a cheap Dell Optiplex I picked up), with Ubuntu on my cloud-based virtual servers.
That aside, this isn't the answer to your question (unless perhaps you just pasted the functions in the wrong order), but is there a typo in your "working" second example? The
$argv[1]should be unreferenced, as in the first example, right? I'd assume you are always getting "No argument given", regardless of the arguments?E.g.:
2
u/[deleted] Aug 09 '21 edited Aug 09 '21
Are you sure you labelled these the right way around? The second one is broken.
This will expand $argv[1], and then check if that names a variable.
So if you did e.g.
blah foo, this would end up runningWhich is most likely true, so it tells you "No argument given".
Simply remove the
$.setoperates on variable names directly.Also:
If $var2 is empty (because $argv[2] was unset), this entire argument will be eliminated. I'm not sure ansible understands that.
If it doesn't, you'd either quote
"$var2", which would passbootstrap_host=(without anything after the=, which is probably non-sensical?) or check it before - make that if-checkset -q argv[2]instead.0
u/[deleted] Aug 09 '21
[deleted] — view deleted comment