Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I translate this bash function to fish shell?

function run () {
    nohup python $1 > nohup.out &
}

On the command line I call this as "run scriptname.py" and bash executes the following command:

python scriptname.py > nohup.out & 

Can you help me translate this to fish.

I have this so far..

function run 
    bash -c "nohup python $1 > nohup.out &"
end

When I call source on ~/.config/fish/config.fish

This exists simply saying

Error when reading file: ~/.config/fish/config.fish 

without providing any helpful hints as to what the error is.

like image 647
jason Avatar asked Feb 07 '23 08:02

jason


2 Answers

There's really no need to execute bash here, fish can also execute nohup, the redirections also work and such.

There's a minor difference in that, instead of $1 and $2 and so on, arguments to fish functions are stored in the $argv array.

function run
     nohup python $argv > nohup.out &
end

This will expand $argv to all elements of that as one element each, so run script.py banana would run nohup python script.py banana > nohup.out &. If you truly want just one argument to be passed, you'd need $argv[1].

I actually have no idea why your definition should cause an error when sourcing config.fish - which fish version are you using?

like image 124
faho Avatar answered Feb 08 '23 22:02

faho


This is a perfectly valid (and more correct) replacement for your function in fish:

function run
    bash -c 'nohup python "$@" > nohup.out &' _ $argv
end

This is an equivalent to the native-bash function:

run() {
  nohup python "$@" </dev/null >nohup.out 2>&1 &
}

...which, personally, I would suggest rewriting to use disown rather than nohup.


With respect to the error seen from fish, I'd suggest paying attention to any other (not syntax-related) which may have impacted whether your file could be read -- permissions, etc.

like image 33
Charles Duffy Avatar answered Feb 08 '23 21:02

Charles Duffy