Web lists-archives.com

[SOLVED] Re: Bash file to variable string problem -- must be simple. What am I missing?





On 3/2/19 10:22 PM, der.hans wrote:
Am 02. Mar, 2019 schwätzte deb so:

moin moin,

rather than the double-reverse, try the truncate operator.

basename=${fname%.*}

$ ( fname=fred.mp4; echo ${fname%.*} )
fred
$ ( fname=fred.georg.mp4; echo ${fname%.*} )
fred.georg
$ ( fname=fred.txt; echo ${fname%.*} )
fred
$ ( fname=fred; echo ${fname%.*} )
fred
$

'%' says to look for the named pattern at the end of the value.

In this case '.*' says the last dot and everything after it if there is a
period in the value.

'%' is not greedy, so will match as little as possible, use '%%' to get
greedy if you need it.

ciao,

der.hans


On 3/2/19 8:07 PM, Roberto C. Sánchez wrote:
On Sat, Mar 02, 2019 at 07:56:58PM -0500, deb wrote:
    This has to be simple and I'm just missing it.

    If I pull a filename from a temp file into a variable, I can ls it fine.

    If I cut off the extension, and tack on my own SAME EXT, ls no longer
    works.

    (The actual script is more elaborate, loading vlc , etc -- but this
    summarizes & shows my issue)

    # mp4file.txt holds just 'long file with spaces.mp4'

    fname=$(<mp4file.txt)

    # echo $fname shows the right filename.mp4 string

    # works
    ls -al "$fname"

    # Cut off the extension.
    fname=`echo $fname | rev | cut -d. -f2 | rev`

    # echo $fname shows the filename sans '.mp4'

    # THIS LS FAILS, WITH FILE NOT FOUND (but actually reports the exact
    string that worked above, but not being found here).

    ls -al "$fname".mp4

    ls: cannot access 'long file with spaces.mp4': No such file or directory

I cannot replicate the behavior you describe.  Here is how it looks for
me:

root@chroot:~# touch "long file with spaces.mp4"
root@chroot:~# echo "long file with spaces.mp4" >mp4file.txt
root@chroot:~# cat mp4file.txt
long file with spaces.mp4
root@chroot:~# ls -l
total 4
-rw-r--r-- 1 root root  0 Mar  3 01:02 long file with spaces.mp4
-rw-r--r-- 1 root root 26 Mar  3 01:02 mp4file.txt
root@chroot:~# fname=$(<mp4file.txt)
root@chroot:~# ls -al "$fname"
-rw-r--r-- 1 root root 0 Mar  3 01:02 long file with spaces.mp4
root@chroot:~# fname=`echo $fname | rev | cut -d. -f2 | rev`
root@chroot:~# ls -al "$fname".mp4
-rw-r--r-- 1 root root 0 Mar  3 01:02 long file with spaces.mp4

What version of bash are you using?

    ------------------------------------------------------

    It is not:

    * a special character thing,

    * a carriage return thing,

    * a character case thing,

    * not helped with './' or '~/' added in front of the filename.

    * It's the same string in both spots.

    Any thoughts folks?

I am not sure about the overall problem, but I can say I would replace
this:

fname=`echo $fname | rev | cut -d. -f2 | rev`

with this:

fname=$(basename "$fname" .mp4)

Regards,

-Roberto

*
*

*Thank you Roberto.*


# Cut off the extension.
# fname=`echo $fname | rev | cut -d. -f2 | rev`

fname=$(basename "$fname" .mp4)

^ this does work for the *ls*, but I do not know that it will be a .mp4.


(It could be a .mkv, .webm, .ogg, .mp4, etc.)

What is certain is the filename to  the left of the final '.'.

So I was building up the different choices to file test for, hence the fname=`echo $fname | rev | cut -d. -f2 | rev`




I'm running:

cat /etc/issue
9.8

bash --version
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)





This did it!


fname=${fname%.*}


# Works perfectly

Thank you der.hans!


(I am working through these responses in delivered order -- but this was the first one to work).