1

I have the following command:

!nohup fswatch -o ~/Notes -e "\\.git.*" | xargs -n 1 -I {} sh ~/auto_commit.sh > /dev/null 2>&1 &

I want to inline what's in the auto_commit.sh file:

cd ~/Notes
git add -A
git commit --allow-empty-message -am ""

Is there a way to do this?

7
  • If I replace sh ~/auto_commit.sh with cd ~/Notes && git add -A && git commit --allow-empty-message -am "" it doesn't work. I'd like to avoid using an external script file and do it all inline. Commented Sep 11, 2017 at 21:19
  • Can you start your nohup in the ~/Notes folder and use git commit -A ..., making it all one command ? Commented Sep 11, 2017 at 21:30
  • I'd prefer not to since this command is fired off by a vim event, so it's in a vim config that's not located in the ~/Notes dir. I suppose I can put that config in there and symlink it, but not sure what the system will think the PWD is. Commented Sep 11, 2017 at 21:33
  • could you run it in a subshell? {cd ~/Notes; git add -A; git commit --allow-empty-message -am ""} Commented Sep 11, 2017 at 22:10
  • I was wrong, but I helped I guess. { ...; } is not a subshell and you cannot really use a subshell as the target of xargs afaik Commented Sep 12, 2017 at 18:08

1 Answer 1

1

The solution I ended up with was:

nohup fswatch -o ~/Notes -e "\\.git.*" | xargs -n 1 -I {} sh -c 'cd ~/Notes; git add -A; git commit --allow-empty-message -m ""' > /dev/null 2>&1 &'

Thanks to Will Barnwell and whjm for your help.

Also, this was for a vim script, so the formatting had to be slightly different (in case others are looking for a solution to a similar problem):

autocmd BufWinEnter *.note silent! execute '!nohup fswatch -o . -e "\\.git.*" | xargs -n 1 -I {} sh -c ''git add -A; git commit --allow-empty-message -m ""'' > /dev/null 2>&1 &'

Note that where Will Barnwell was wrong is that the {..} is not a subshell, and the sh -c command expects a string. So the curly braces were unnecessary, and in my case added an undesired git commit message.

Also, when used in this vim script with the vim execute function, the entire command had to be wrapped in quotes. Doubling up on single quotes (not double quotes mind you) allowed the execute function to accept the argument as a normal string, and things worked fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.