carpal tunnel
Registered: 29/08/2000
Posts: 14506
Loc: Canada
|
I'm sure a real shell wizard could do a lot better, but at least this is bulletproof: #!/bin/sh
# capitalize first letter of all items in current directory:
ls -1 | awk '{gsub("\"","\\\""); system("mv \"" $0 "\" \"" toupper(substr($0,1,1)) substr($0,2) "\"")}' Translated to human-speak, it breaks down as follows: - Standard shell script header:
#!/bin/sh - List directory contents, one item per line:
ls -1 - Feed the directory listing into an awk program:
| awk '{ ... }' - Insert a backslash in front of any/all double-quotes within file names, so that we can then safely use then inside double-quoted expressions in the program:
gsub("\"","\\\""); - Execute a new command line, using mv to rename things:
system("mv " OLD " " NEW) - OLD and NEW are in this case, double-quoted file names. The OLD name is simply the current line of input to the awk program, known as $0. Double-quoted, this becomes:
"\"" $0 "\"" - The NEW name is built from two parts: the first character of the OLD name, substr($0,1,1), and the rest of the OLD name, substr($0,2). Again, double-quoted:
"\"" toupper(substr($0,1,1)) substr($0,2) "\"" - Put it all together, and we see this:
system("mv " "\"" $0 "\"" " " "\"" toupper(substr($0,1,1)) substr($0,2) "\"") - We can collapse adjacent double-quoted strings together, to simplify it somewhat:
system("mv \"" $0 "\" \"" toupper(substr($0,1,1)) substr($0,2) "\"")
And that's the final result. 
|