0

In my previous question I created a command that reverts svn files by pattern:

svn st | grep SomeFolderA | awk {'print $2'} | xargs svn revert

Now I want to make it a command that accepts argument (file pattern) and runs the above command by using the argument instead of SomeFolderA. I tried adding this to my .bash_profile file:

function revert() {
    "svn st | grep '$1' | awk '{print \$2}' | xargs svn revert" ;
}

When I run revert SomeFolderA I get this output:

-bash: svn st | grep 'SomeFolderA/' | awk '{print $2}' | xargs svn revert: No such file or directory

I also tried using the complete path for the svn but it still doesn't work. What am I missing?

Community
  • 1
  • 1
Avi
  • 21,182
  • 26
  • 82
  • 121
  • `"svn st | grep '$1' | awk '{print \$2}' | xargs svn revert"` tries to execute a command with that name. Remove the quotes.See http://www.linuxjournal.com/content/return-values-bash-functions on how bash functions return values. – ivan_pozdeev Oct 15 '13 at 07:44
  • possible duplicate of [How to return a string value from a bash function](http://stackoverflow.com/questions/3236871/how-to-return-a-string-value-from-a-bash-function) – ivan_pozdeev Oct 15 '13 at 07:48
  • @ivan_pozdeev - I've done some digging and I admit bash scripting is far from being my strongest side. I think it's easy to see that it's not a question written with no thought behind it. SO comes to solve exactly these type of issues, not only hard-never-seen-before problems. We use the community to improve our work faster. By getting this answer you help me both to spare some hours and learn. I really don't see how the question you posted is a duplicate. Guess I'm noobier than I though reg bash scripting. – Avi Oct 15 '13 at 08:02
  • sorry for that, the question really looked too trivial to believe any research had been done. Maybe it's just me being too hardcore at digging problems myself before polluting the internet with them. – ivan_pozdeev Oct 15 '13 at 08:51
  • @ivan_pozdeev - Too trivial and yet your solution doesn't work nor other solutions suggested here. Perhaps, just perhaps, it's not SO trivial. Especially for someone who doesn't script bash all day long (actually very rarely do). I'd invest in helping the community instead of polluting it with sarcastic remarks. Or at least move on to the next question. Each such polluting issue is another google search that someone would do and find his/hers solution quicker. – Avi Oct 15 '13 at 14:44

2 Answers2

1

Create a variable with your command string and execute it

function revert() {
  cmd="svn st | grep '$1' | awk '{print \$2}' | xargs svn revert"
  eval $cmd
}
LMC
  • 10,453
  • 2
  • 27
  • 52
  • It didn't work. But I added `eval $cmd` (instead of just `$cmd`) and it works. Fix your answer to include this line and I'll accept it. – Avi Oct 15 '13 at 14:31
0

No need to use function, put off the function, just use below in you script :

svn st | grep "$1" | awk '{print \$2}' | xargs svn revert
Nancy
  • 1,183
  • 1
  • 7
  • 17
  • I tried this but getting this syntax error: `awk: syntax error at source line 1 context is >>> <<< awk: illegal statement at source line 1 missing }` Have any idea? – Avi Oct 15 '13 at 10:37
  • 1
    The `$` is escaped twice, once by being inside the single quotes and again with the backslash. The backslash can be omitted. – chepner Oct 15 '13 at 12:30
  • 1
    @Cheers The function is the key part of the question: it is defined in `.bash_profile` so that it can be used at the command line without having to type out the entire pipeline. – chepner Oct 15 '13 at 12:31