|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Using bash I want to rename a file to have a date suffix. I can do
that in an interactive shell with this command: mv testfile !#:1.$(date "-I") I end up with testfile.2007-07-13 which is what I want. If I put the same command in a bash script, it fails. Why is that? The !#:1 is taken literally as part of the new name. No amount of escaping with backslashes s. Can some explain this to me in simple terms? Tell me how to make it work in a script? Thanks for all explanations. --Bob |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 2007-07-13, R B Love wrote:
> Using bash I want to rename a file to have a date suffix. I can do > that in an interactive shell with this command: > > > mv testfile !#:1.$(date "-I") > > I end up with testfile.2007-07-13 which is what I want. > > If I put the same command in a bash script, it fails. Why is that? > The !#:1 is taken literally as part of the new name. No amount of > escaping with backslashes s. > > Can some explain this to me in simple terms? Tell me how to make it > work in a script? That doesn't work for me either at the command line or in a script. (I remove the special meaning of ! by setting histchars to an empty string.) In a script, I presume you would be using a command-line parameter instead of hard-coding testfile. In that case, this would work: mv "$1" "$1.$(date "-I")" -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
In article <2007071317031616807-rblove@airmailnet>,
R. B. Love <rblove@airmail.net> wrote: > Using bash I want to rename a file to have a date suffix. I can do > that in an interactive shell with this command: > > > mv testfile !#:1.$(date "-I") > > I end up with testfile.2007-07-13 which is what I want. > > If I put the same command in a bash script, it fails. Why is that? > The !#:1 is taken literally as part of the new name. No amount of > escaping with backslashes s. !#:1 is a history substitution, but history is normally only enabled in interactive shells, not shells running scripts. > > Can some explain this to me in simple terms? Tell me how to make it > work in a script? Isn't the filename to work on in a variable? Just use that variable: filename=testfile mv $filename $filename.$(date -I) -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group *** |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 2007-07-13 20:15:14 -0500, Barry Margolin <barmar@alum.mit.edu> said:
> In article <2007071317031616807-rblove@airmailnet>, > R. B. Love <rblove@airmail.net> wrote: > >> Using bash I want to rename a file to have a date suffix. I can do >> that in an interactive shell with this command: >> >> >> mv testfile !#:1.$(date "-I") >> >> I end up with testfile.2007-07-13 which is what I want. >> >> If I put the same command in a bash script, it fails. Why is that? >> The !#:1 is taken literally as part of the new name. No amount of >> escaping with backslashes s. > > !#:1 is a history substitution, but history is normally only enabled in > interactive shells, not shells running scripts. History? OK, but it seems that the command hasn't been executed when it goes to evaluate the !#:1 so there should be no history. > >> >> Can some explain this to me in simple terms? Tell me how to make it >> work in a script? > > Isn't the filename to work on in a variable? Just use that variable: Yes, I certainly can use variables, I was just trying to understand why the example I'd learned from didn't work in a script. Thanks to folks for the advice. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
2007-07-13, 21:15(-04), Barry Margolin:
[...] > Isn't the filename to work on in a variable? Just use that variable: > > filename=testfile > mv $filename $filename.$(date -I) That's not how you use string variables in shell. Above, you need double quotes around then, otherwise they are considered as lists. Same thing for command substitution. mv -- "$filename" "$filename.$(date -I)" If you wanted to be portable, you'd rather use date +%Y-%m-%d than date -I. -- Stéphane |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
In article <2007071322260116807-rblove@airmailnet>,
R. B. Love <rblove@airmail.net> wrote: > On 2007-07-13 20:15:14 -0500, Barry Margolin <barmar@alum.mit.edu> said: > > > In article <2007071317031616807-rblove@airmailnet>, > > R. B. Love <rblove@airmail.net> wrote: > > > >> Using bash I want to rename a file to have a date suffix. I can do > >> that in an interactive shell with this command: > >> > >> > >> mv testfile !#:1.$(date "-I") > >> > >> I end up with testfile.2007-07-13 which is what I want. > >> > >> If I put the same command in a bash script, it fails. Why is that? > >> The !#:1 is taken literally as part of the new name. No amount of > >> escaping with backslashes s. > > > > !#:1 is a history substitution, but history is normally only enabled in > > interactive shells, not shells running scripts. > > History? OK, but it seems that the command hasn't been executed when > it goes to evaluate the !#:1 so there should be no history. From the HISTORY EXPANSION section of the man page: Event Designators An event designator is a reference to a command line entry in the history list. .... !# The entire command line typed so far. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group *** |
|
![]() |
| Outils de la discussion | |
|
|