2

I run fish shell and it gives an exception when I try to remove a file which is not found. I have the following script:

#!/usr/bin/fish

set files "/tmp/*.xlsx"
rm  -f "$files"
echo "$files"
set tmpfile (mktemp -u).xlsx
curl http://localhost:18085/myService -o "$tmpfile"  -s
xdg-open "$tmpfile"

When I run it like this than the files variable has a complete list of all the patterns. But if one of them cannot be deleted (e.g. still opened) - none would be delete.

I have two questions:

  1. What is the best way to delete the files?
  2. Since I'm developing sometimes the file isn't downloaded by the curl. How to check and only open if the download was successful? (check if exists or check if file size >0 or??)
2
  • maybe instead of (mktemp -u).xlsx you mean $(mktemp -u).xlsx Jul 28, 2021 at 15:52
  • 3
    @Romeo, no this is fish. Command substitution has slightly different syntax. Jul 28, 2021 at 15:57

1 Answer 1

4

It looks like you're programming fish like you would program sh. Try to "free your mind" of sh limitations.

  1. fish variable values are lists of strings: I would expand the glob and store the list of files -- i.e. remove the quotes:
    set files /tmp/*.xlsx
    
  2. then you provide the expanded list of files to rm
    rm  -f $files
    

Unrelated to the above: you can ask mktemp to append the suffix (assuming GNU coreutils)

set tmpfile (mktemp -u --suffix=.xlsx)

and then $tmpfile can be used without quotes.

1
  • Thx for your hints. I'll check tomorrow. You have also an idea how to check properly if the file exists ==> run xdg-open?
    – LeO
    Jul 28, 2021 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.