Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi condition if statements can produce some very wierd behaviour #1583

Closed
taksuyu opened this issue Jul 27, 2014 · 4 comments
Closed

Multi condition if statements can produce some very wierd behaviour #1583

taksuyu opened this issue Jul 27, 2014 · 4 comments
Milestone

Comments

@taksuyu
Copy link

taksuyu commented Jul 27, 2014

function check_path
    if test -d $argv[1]
        echo "$argv[1] made it through test"
        if not contains $argv[1] $PATH
            echo "$argv[1] will be added"
            set PATH $argv[1] $PATH
        end
    end
end

Works, but

function check_path
    if test -d $argv[1]; and not contains $argv[1] $PATH
            set PATH $argv[1] $PATH
    end
end

Doesn't

@taksuyu taksuyu changed the title Multi condition if statements can produce some vary wierd behaviour Multi condition if statements can produce some very wierd behaviour Jul 27, 2014
@ridiculousfish
Copy link
Member

Your expectation for how and behaves is very reasonable, but it's not so. The loop condition is a single command, and so the and does not attach to it, i.e. it's parsed as:

if test -d $argv[1]
    and not contains $argv[1] $PATH
    set PATH $argv[1] $PATH
end

To get the behavior you want, you have to group them with begin/end:

 if begin; test -d $argv[1]; and not contains $argv[1] $PATH; end
     set PATH $argv[1] $PATH
 end

which is definitely awkward.

it might be worth producing a warning in the case when 'and' follow an if statement immediately, or possibly just fixing it so it works like you would want, or supporting && (see #150).

@taksuyu
Copy link
Author

taksuyu commented Jul 28, 2014

I actually tried it with a test statement too, which produced strange results

if test -d $argv[1] -a ! contains $argv[1] $PATH
if test -d $argv[1] -a (not contains $argv[1] $PATH)

but yeah, I agree with you.

I'd actually like to see && and || specifically for if, but I imagine that a lot of people make the mistake I do with and.

@faho
Copy link
Member

faho commented Jul 16, 2015

See also #1428.

@faho
Copy link
Member

faho commented Apr 4, 2016

This should be fixed like #1428.

@faho faho closed this as completed Apr 4, 2016
@zanchey zanchey modified the milestones: 2.3.0, fish-future Apr 5, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants