Search code examples
linuxpromptfish

Problem using ";and" in fish when clearing cache in my linux system


I'm trying to create a function that clears my cache in my system according to this page so I need to add the following commands in a function:

1. Clear PageCache only.

 sync; echo 1 > /proc/sys/vm/drop_caches

2. Clear dentries and inodes.

 sync; echo 2 > /proc/sys/vm/drop_caches

3. Clear PageCache, dentries and inodes.

 sync; echo 3 > /proc/sys/vm/drop_caches 

I wanted to make use of "; and" in the function, separating each of those commands so in case that there's an error the next command doesn't get executed. According to fish documentation, you use COMMAND1; and COMMAND2 for that. So I did this:

function clearcache
sudo sh -c 'sync; echo 1 > /proc/sys/vm/drop_caches ;and sync; echo 2 > /proc/sys/vm/drop_caches; and sync; echo 3 > /proc/sys/vm/drop_caches'

end

But when I execute it, I get the following error or warning:

sh: 1: and: not found
sh: 1: and: not found

I then tried to remove the "and" from the syntax to see if that did something different and check if that was the problem and in fact I got it to work.

user@debian64 /etc> sudo sh -c 'sync; echo 1 > /proc/sys/vm/drop_caches ; sync; echo 2 > /proc/sys/vm/drop_caches; sync; echo 3 > /proc/sys/vm/drop_caches'
user@debian64 /etc>

My question is, how can I get advantage of the "and" feature in fish, or what am I doing wrong? Thank you


Solution

  • Your definition for function clearcache runs the command under sh -c. sh is not fish; it uses && for the functionality that fish calls ; and.

    That being said, what you're doing is redundant. Writing 3 to /proc/sys/vm/drop_caches has the same effect as writing 1 and 2.