Nathan
Nathan

Reputation: 7855

How to combine multiple commands when piped through xargs?

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?

Upvotes: 1

Views: 185

Answers (1)

Nathan
Nathan

Reputation: 7855

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.

Upvotes: 1

Related Questions