by dllu · 8y ago · 0 comments
Unfortunately, although it is fully scriptable, writing scripts in Fish feels a little clunky at times. For example, I wanted to test if $dest exists, and if not, whether $src is newer than $dest. Since there are no && or ||, parentheses are used for command substitution, and the Fish version of "test" has no -nt to check if a file is newer, I have to do this:
if begin; not command test -f $dest; end; or begin; command test $src -nt $dest; end
# do something
endHappily the begin/ends in ifs are no longer necessary:
if not command test -f $dest
or command test $src -nt $dest
# stuff
end
In this particular case, you can simplify it further with test's operators: if command [ ! -f $dest -o $src -nt $dest ]
#stuff
end
which is comparable in length to bash, though other cases will certainly be longer.What I ended up with was (for a shortcut for generating a password on the command line):
function pw --argument length
test -z $length; and set length 16
python3.6 -c "import secrets; print(secrets.token_urlsafe($length))"
end not command test -f $dest
if or command test $src -nt $dest
# do something
end
It saves on the begin ... end, but is confusing to look at. Nested conditions are really not very ergonomic in fish.