Comments on: Subversion (SVN) helper bash script to list repo dirs http://www.movingtofreedom.org/2007/10/14/subversion-svn-helper-bash-script-to-list-repository-directories/ free software, free culture, free association Sat, 30 Aug 2008 00:02:58 +0000 http://wordpress.org/?v=2.0.11 by: Scott Carpenter http://www.movingtofreedom.org/2007/10/14/subversion-svn-helper-bash-script-to-list-repository-directories/#comment-3463 Thu, 21 Feb 2008 23:35:46 +0000 http://www.movingtofreedom.org/2007/10/14/subversion-svn-helper-bash-script-to-list-repository-directories/#comment-3463 Excellent -- thanks, Christopher. I'm still pretty clumsy with bash, which may be mostly me needing to learn more about the basics, but I think also bash has its limitations for scripting. I'm learning Python now, and imagine my future attempts at utilities like this will be done with it instead. But I still think bash is awesome! Excellent — thanks, Christopher. I’m still pretty clumsy with bash, which may be mostly me needing to learn more about the basics, but I think also bash has its limitations for scripting.

I’m learning Python now, and imagine my future attempts at utilities like this will be done with it instead.

But I still think bash is awesome!

]]>
by: Christopher http://www.movingtofreedom.org/2007/10/14/subversion-svn-helper-bash-script-to-list-repository-directories/#comment-3462 Thu, 21 Feb 2008 23:10:38 +0000 http://www.movingtofreedom.org/2007/10/14/subversion-svn-helper-bash-script-to-list-repository-directories/#comment-3462 This may be premature optimization in this case, but in Bash you can use arithmetic inside parameter expansion. This can be used to get the last character in a variable, which you did in three steps. <code>${inpt:${#inpt}-1}</code> This expands into the last character of <code>inpt</code> using only one parameter expansion. As a bonus (?), it makes the code almost as unreadable as Perl. :) This may be premature optimization in this case, but in Bash you can use arithmetic inside parameter expansion. This can be used to get the last character in a variable, which you did in three steps.

${inpt:${#inpt}-1}

This expands into the last character of inpt using only one parameter expansion. As a bonus (?), it makes the code almost as unreadable as Perl. :)

]]>
by: Scott Carpenter http://www.movingtofreedom.org/2007/10/14/subversion-svn-helper-bash-script-to-list-repository-directories/#comment-3272 Mon, 15 Oct 2007 21:11:38 +0000 http://www.movingtofreedom.org/2007/10/14/subversion-svn-helper-bash-script-to-list-repository-directories/#comment-3272 Excellent -- thanks, Mario. This is the kind of stuff I like to hear about. I have a book on sed and awk on my desk at work and wish I had more time to work through it so I might get these ideas in advance. I also wish I had spent the last 10 years learning this stuff instead of wasting my time in Windows. :-) I figure it's still good to struggle and figure it out one way or another, and try not to be paralyzed at the thought of all the better, quicker ways something might be done. I think I gained a little better understanding of how the pipeline works by figuring out how to use "read" in the bash script. Anyway, I appreciate you taking the time to explain alternate options. I always want to understand my options better and pick the most suitable approach. Excellent — thanks, Mario. This is the kind of stuff I like to hear about.

I have a book on sed and awk on my desk at work and wish I had more time to work through it so I might get these ideas in advance. I also wish I had spent the last 10 years learning this stuff instead of wasting my time in Windows. :-)

I figure it’s still good to struggle and figure it out one way or another, and try not to be paralyzed at the thought of all the better, quicker ways something might be done. I think I gained a little better understanding of how the pipeline works by figuring out how to use “read” in the bash script.

Anyway, I appreciate you taking the time to explain alternate options. I always want to understand my options better and pick the most suitable approach.

]]>
by: Mario Stargard http://www.movingtofreedom.org/2007/10/14/subversion-svn-helper-bash-script-to-list-repository-directories/#comment-3270 Mon, 15 Oct 2007 15:10:30 +0000 http://www.movingtofreedom.org/2007/10/14/subversion-svn-helper-bash-script-to-list-repository-directories/#comment-3270 Unix scripting is interesting in that there are many solutions to the same problem. Solutions come from perspective. I would have used awk to solve the same problem. Others would have used perl. Within bash, there are ways to solve this without calling external executables, such as wc or tr. Here's what I would have done: <code> svn list -R file:///home/scarpent/src/svn |awk -F/ '$NF ~ /^$/ && NF <= 2' </code> awk is kind of like perl-lite. It is useful for parsing lines into fields at a separator. In this example, "/" is the separator. awk places the stuff between the separators into fields, which are addressable as $1, $2, etc. The number of fields is in the variable NF, and the contents of the last field are in $NF. So, the awk statement parses the line (record in awk parlance) into fields at "/", then for records that have no contents in the last field (i.e. the ones with a trailing "/"), and have a number of fields less than or equal to 2, print the line. The print is implied. If you want to vary the number of fields to match, you can pass variables into awk using the -v switch: <code> svn list -R file:///home/scarpent/src/svn |awk -v NUM=2 -F/ '$NF ~ /^$/ && NF <= NUM' </code> which might make it more suitable for putting into a shell script. Unix scripting is interesting in that there are many solutions to the same problem. Solutions come from perspective. I would have used awk to solve the same problem. Others would have used perl. Within bash, there are ways to solve this without calling external executables, such as wc or tr. Here’s what I would have done:


svn list -R file:///home/scarpent/src/svn |awk -F/ '$NF ~ /^$/ && NF <= 2'

awk is kind of like perl-lite. It is useful for parsing lines into fields at a separator. In this example, “/” is the separator. awk places the stuff between the separators into fields, which are addressable as $1, $2, etc. The number of fields is in the variable NF, and the contents of the last field are in $NF.

So, the awk statement parses the line (record in awk parlance) into fields at “/”, then for records that have no contents in the last field (i.e. the ones with a trailing “/”), and have a number of fields less than or equal to 2, print the line. The print is implied.

If you want to vary the number of fields to match, you can pass variables into awk using the -v switch:


svn list -R file:///home/scarpent/src/svn |awk -v NUM=2 -F/ '$NF ~ /^$/ && NF <= NUM'

which might make it more suitable for putting into a shell script.

]]>