Mailing List Archive

Script quoting problem
Hi,

I want to rename the files in "show_names", created by mythlink.pl.

I have in a test directory:

$ ls
1234.ts 5678.ts film1.mpg film2.mpg

I wish to rename 1234.ts in film1.ts and 5678.ts in film2.ts.

I have a quoting problem with my script.

My script (script.sh) is:

----------------
# Cut everything after the point
oldname=$(ls *.ts | cut -d. -f1)
newname=$(ls *.mpg | cut -d. -f1)

# Rename file
for file in *.ts; do
mv $file $(echo $file | sed 's/$oldname/$newname/g')
Done
----------------

$ bash script.sh
mv: '1234.ts' et '1234.ts' are the same file
mv: '5678.ts' et '5678.ts' are the same file

However "echo $oldname" shows "1234 5678" (that's what I want), and "echo
$newname" shows "film1 film2".

I searched on the web and i tried other quoting manners I found there, but I
can't get it work as I want.

I am beginner in scripting and I can't find the error.

It's just a simplified example script. Once it works, it should be easy to
make it work for MythTV recordings.

Bye

Klaus




_______________________________________________
mythtv-users mailing list
mythtv-users@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-users
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: Script quoting problem [ In reply to ]
Le samedi 7 avril 2018, 20:45:04 CEST Klaus Becker a ?crit :
> Hi,
>
> I want to rename the files in "show_names", created by mythlink.pl.
>
> I have in a test directory:
>
> $ ls
> 1234.ts 5678.ts film1.mpg film2.mpg
>
> I wish to rename 1234.ts in film1.ts and 5678.ts in film2.ts.
>
> I have a quoting problem with my script.
>
> My script (script.sh) is:
>
> ----------------
> # Cut everything after the point
> oldname=$(ls *.ts | cut -d. -f1)
> newname=$(ls *.mpg | cut -d. -f1)
>
> # Rename file
> for file in *.ts; do
> mv $file $(echo $file | sed 's/$oldname/$newname/g')
> Done
> ----------------
>
> $ bash script.sh
> mv: '1234.ts' et '1234.ts' are the same file
> mv: '5678.ts' et '5678.ts' are the same file
>
> However "echo $oldname" shows "1234 5678" (that's what I want), and "echo
> $newname" shows "film1 film2".
>
> I searched on the web and i tried other quoting manners I found there, but I
> can't get it work as I want.
>
> I am beginner in scripting and I can't find the error.
>
> It's just a simplified example script. Once it works, it should be easy to
> make it work for MythTV recordings.
>
> Bye
>
> Klaus


I made a mistake:

I want to give the files in my recording directory the names in "show_names".
But my above question still is the same.

Klaus


_______________________________________________
mythtv-users mailing list
mythtv-users@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-users
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: Script quoting problem [ In reply to ]
—cut--

>
> My script (script.sh) is:
>
> ----------------
> # Cut everything after the point
> oldname=$(ls *.ts | cut -d. -f1)
> newname=$(ls *.mpg | cut -d. -f1)
>
> # Rename file
> for file in *.ts; do
I think this is your problem
> mv $file $(echo $file | sed 's/$oldname/$newname/g’)

on the first iteration around the loop you are echoing 1234.ts and piping it into sed, the problem is you are asking sed to replace “1234 5678” with “film1 film2” as sed doesn’t find a match it returns "1234.ts” again.

I’m guessing what you are trying to achieve isn’t as straight forward as just renaming two files, if you can describe your aim I’ll try and help you with the script.

Regards
Russell
_______________________________________________
mythtv-users mailing list
mythtv-users@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-users
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: Script quoting problem [ In reply to ]
Le samedi 7 avril 2018, 21:25:49 CEST Russell Gower a écrit :
> —cut--
>
> > My script (script.sh) is:
> >
> > ----------------
> > # Cut everything after the point
> > oldname=$(ls *.ts | cut -d. -f1)
> > newname=$(ls *.mpg | cut -d. -f1)
> >
> > # Rename file
> > for file in *.ts; do
>
> I think this is your problem
>
> > mv $file $(echo $file | sed 's/$oldname/$newname/g’)
>
> on the first iteration around the loop you are echoing 1234.ts and piping it
> into sed, the problem is you are asking sed to replace “1234 5678” with
> “film1 film2” as sed doesn’t find a match it returns "1234.ts” again.
>
> I’m guessing what you are trying to achieve isn’t as straight forward as
> just renaming two files, if you can describe your aim I’ll try and help you
> with the script.
>
> Regards
> Russell


Hi Russell,

I use mythlink.pl - https://www.mythtv.org/wiki/Mythlink.pl - which creates
human readable filenames of recorded episodes in "show_names" directory.

Example:

In my recording directory I have filenames like
11326_20180404132300.ts 11301_20180404181500.ts

Mythlink.pl creates a subdirectory "show_names" with flienames like
'Fight Club der Tiere.ts' 'Opa wird Papa.ts' which point to the above files.

This is much better than the original filenames created by MythTV in my
recording directory, but it would be even better if the filenames in the
recording directory are human readable. I only want the name of the episode,
nothing else.

So I would like to have in my recording directory 'Fight Club der Tiere.ts'
and 'Opa wird Papa.ts' instead of 11326_20180404132300.ts and
11301_20180404181500.ts.

Cheers

Klaus




_______________________________________________
mythtv-users mailing list
mythtv-users@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-users
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: Script quoting problem [ In reply to ]
On 8 April 2018 5:18:27 am Klaus Becker <colonius@free.fr> wrote:



So I would like to have in my recording directory 'Fight Club der Tiere.ts'
and 'Opa wird Papa.ts' instead of 11326_20180404132300.ts and
11301_20180404181500.ts.

Cheers

Klaus




___________________________________________

I presume this is been taken as a given but I will mention it anyway -
Doing this will make the files unplayable in MythTV Recordings because it
will no longer be able to find them.


_______________________________________________
mythtv-users mailing list
mythtv-users@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-users
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: Script quoting problem [ In reply to ]
On Sat, 2018-04-07 at 20:45 +0200, Klaus Becker wrote:
> My script (script.sh) is:
>
> ----------------

In general you should start all shell scripts with "set -e" to cause
errors to lead to an exit. Using "set -x" in addition can help you see
what is going on.

> # Cut everything after the point
> oldname=$(ls *.ts | cut -d. -f1)
> newname=$(ls *.mpg | cut -d. -f1)

These will be all the basenames in one variable, not just one at a
time. That seems unlikely to be what you want. Also the individual
names will not be quoted, so you would have no way of separating them.
Using echo to examine these variables will demonstrate this.

> # Rename file
> for file in *.ts; do
> mv $file $(echo $file | sed 's/$oldname/$newname/g')
> Done

Neither $oldname nor $newname is in the required regex format (you
would need | between the individidual names), but since you've used
single quotes they won't be substituted anyhow, sed will by trying to
substitute the literal strings "$oldname" and "$newname"...

It seems like what you want to do is to "collapse" the symlinks, that
is to promote the target of the symlink into the place of the symlink
itself. The shell command "readlink" will give you the target of the
symlink which you give to it, so try:

#!/bin/bash

set -ex

for file in *.mpg; do
target=$(readlink "$file")
echo mv -iv "$target" "$file"
done

Run it and if you are happy with what it proposes to do remove the
safety echo.

I would reiterate Mark's comment though -- doing this will make things
unplayable in mythtv and leave stale entries in your myth recording
lists which you will need to cleanup separately.

Ian.

_______________________________________________
mythtv-users mailing list
mythtv-users@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-users
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: Script quoting problem [ In reply to ]
Le dimanche 8 avril 2018, 00:18:35 CEST Mark Perkins a ?crit :
> On 8 April 2018 5:18:27 am Klaus Becker <colonius@free.fr> wrote:
>
>
>
> So I would like to have in my recording directory 'Fight Club der Tiere.ts'
> and 'Opa wird Papa.ts' instead of 11326_20180404132300.ts and
> 11301_20180404181500.ts.
>
> Cheers
>
> Klaus
>
>
>
>
> ___________________________________________
>
> I presume this is been taken as a given but I will mention it anyway -
> Doing this will make the files unplayable in MythTV Recordings because it
> will no longer be able to find them.


I ignored that. In this case I shall let the filenames as they are.

Klaus


_______________________________________________
mythtv-users mailing list
mythtv-users@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-users
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org