{"action":"created","issue":{"url":"https://api.github.com/repos/zfsnap/zfsnap/issues/69","labels_url":"https://api.github.com/repos/zfsnap/zfsnap/issues/69/labels{/name}","comments_url":"https://api.github.com/repos/zfsnap/zfsnap/issues/69/comments","events_url":"https://api.github.com/repos/zfsnap/zfsnap/issues/69/events","html_url":"https://github.com/zfsnap/zfsnap/pull/69","id":118171930,"number":69,"title":"Reset OPTIND in loops that use getopts.","user":{"login":"gsadams","id":4380004,"avatar_url":"https://avatars.githubusercontent.com/u/4380004?v=3","gravatar_id":"","url":"https://api.github.com/users/gsadams","html_url":"https://github.com/gsadams","followers_url":"https://api.github.com/users/gsadams/followers","following_url":"https://api.github.com/users/gsadams/following{/other_user}","gists_url":"https://api.github.com/users/gsadams/gists{/gist_id}","starred_url":"https://api.github.com/users/gsadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsadams/subscriptions","organizations_url":"https://api.github.com/users/gsadams/orgs","repos_url":"https://api.github.com/users/gsadams/repos","events_url":"https://api.github.com/users/gsadams/events{/privacy}","received_events_url":"https://api.github.com/users/gsadams/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-21T05:22:05Z","updated_at":"2015-11-21T05:32:04Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/zfsnap/zfsnap/pulls/69","html_url":"https://github.com/zfsnap/zfsnap/pull/69","diff_url":"https://github.com/zfsnap/zfsnap/pull/69.diff","patch_url":"https://github.com/zfsnap/zfsnap/pull/69.patch"},"body":"In bash, and probably some other shells, failing to reset OPTIND to 1 after shifting the argument list and before calling getopts subsequent times will cause it not to parse any more arguments. The end result is that the scripts would miss any further arguments after the first filesystem name.\r\n\r\nFor instance, if I issued this command:\r\n\r\n  `zfsnap snapshot -r -p weekly- -a 2m tank/backups -a 1m pony media`\r\n\r\nthen snapshots would only be added to tank/backups. That's because the `shift $(($OPTIND - 1))` statement would shift by 5, the script would go on to process the next argument, `tank/backups` and then shift by 1 more. That leaves the final four arguments, but OPTIND would still be at 6 when the next invocation of getopts happens at the top of the loop. So getopts would do nothing, and we'd then shift by 5 again, leaving apparently nothing left to do. I suppose things would get even worse if I had lots of arguments after the first five...\r\n\r\nPerhaps some shells reset OPTIND for each invocation of getopts. Bash, at least, does not."},"comment":{"url":"https://api.github.com/repos/zfsnap/zfsnap/issues/comments/158591825","html_url":"https://github.com/zfsnap/zfsnap/pull/69#issuecomment-158591825","issue_url":"https://api.github.com/repos/zfsnap/zfsnap/issues/69","id":158591825,"user":{"login":"gsadams","id":4380004,"avatar_url":"https://avatars.githubusercontent.com/u/4380004?v=3","gravatar_id":"","url":"https://api.github.com/users/gsadams","html_url":"https://github.com/gsadams","followers_url":"https://api.github.com/users/gsadams/followers","following_url":"https://api.github.com/users/gsadams/following{/other_user}","gists_url":"https://api.github.com/users/gsadams/gists{/gist_id}","starred_url":"https://api.github.com/users/gsadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsadams/subscriptions","organizations_url":"https://api.github.com/users/gsadams/orgs","repos_url":"https://api.github.com/users/gsadams/repos","events_url":"https://api.github.com/users/gsadams/events{/privacy}","received_events_url":"https://api.github.com/users/gsadams/received_events","type":"User","site_admin":false},"created_at":"2015-11-21T05:32:04Z","updated_at":"2015-11-21T05:32:04Z","body":"By the way, I tried to add a test for this case in the integration tests directory, but I couldn't think of a reasonable way to test it using the infrastructure that's there. Hmm."}}