PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > File Move Different In scripting
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

File Move Different In scripting

Réponse
 
LinkBack Outils de la discussion
Vieux 13/07/2007, 23h03   #1
R. B. Love
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut File Move Different In scripting

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

  Réponse avec citation
Vieux 13/07/2007, 23h31   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Move Different In scripting

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
  Réponse avec citation
Vieux 14/07/2007, 02h15   #3
Barry Margolin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Move Different In scripting

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 ***
  Réponse avec citation
Vieux 14/07/2007, 04h26   #4
R. B. Love
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Move Different In scripting

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.

  Réponse avec citation
Vieux 14/07/2007, 11h18   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Move Different In scripting

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
  Réponse avec citation
Vieux 14/07/2007, 21h41   #6
Barry Margolin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Move Different In scripting

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 ***
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h48.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11519 seconds with 14 queries