0

Using Ubuntu 22.04.1 LTS.

I have two scripts (script1.sh and script2.sh).

If I executed either one independently they work properly. If I call script2.sh from script1.sh it errors on a string replacement command in script2.sh. But if I execute script2.sh directly (not called from another script) it works properly. I have removed passing parameters to simplify the example. In the full script, I will pass SrcRoot and DestRoot from script1 to script2.

I am looking for a way to fix this. In my full script, I will not have SrcFullSpec in script1.sh as it is generated inside of the called script (script2.sh), so it is not an option to pass that as a parameter

#!/bin/bash
#script1.sh

SrcRoot=/mnt/media_bulk/movies
DestRoot=/mnt/media_bulk/new-movies-H265
SrcFullSpec="/mnt/media_bulk/movies/subdir 1/American Sniper.m4v"

echo "Sending SrcRoot: $SrcRoot"
echo "Sending DestRoot: $DestRoot"
echo "Sending SrcFullSpec: $SrcFullSpec"

# this is the same string manipulation command in script2.sh
# it works properly here
echo ""
ReplacePath="${SrcFullSpec/${SrcRoot}/${DestRoot}}"
echo "Replace Path: $ReplacePath"

sh script2.sh

Output from script1.sh

$ ./script1.sh
Sending SrcRoot: /mnt/media_bulk/movies

Sending DestRoot: /mnt/media_bulk/new-movies-H265

Sending SrcFullSpec: /mnt/media_bulk/movies/subdir 1/American Sniper.m4v

Replace Path: /mnt/media_bulk/new-movies-H265/subdir 1/American Sniper.m4v

********************
Executing scritp2.sh
********************

LSrcFullSpec: /mnt/media_bulk/movies/subdir 1/American Sniper.m4v

LSrcRoot: /mnt/media_bulk/movies

LDestRoot: /mnt/media_bulk/new-movies-H265

**script2.sh: 18: Bad substitution**

Script2.sh source code

#!/bin/bash
#script2.sh

LSrcRoot=/mnt/media_bulk/movies
LDestRoot=/mnt/media_bulk/new-movies-H265
LSrcFullSpec="/mnt/media_bulk/movies/subdir 1/American Sniper.m4v"

    echo ""
    echo "********************"
    echo "Executing scritp2.sh"
    echo "********************"
    echo ""

    echo "LSrcFullSpec: $LSrcFullSpec"
    echo "LSrcRoot: $LSrcRoot"
    echo "LDestRoot: $LDestRoot"
    echo ""
    NewPath="${LSrcFullSpec/${LSrcRoot}/${LDestRoot}}"
    echo "New Spec: $NewPath"

Output from executing script2.sh by itself

$ ./script2.sh

********************
Executing scritp2.sh
********************

LSrcFullSpec: /mnt/media_bulk/movies/subdir 1/American Sniper.m4v

LSrcRoot: /mnt/media_bulk/movies

LDestRoot: /mnt/media_bulk/new-movies-H265

New Spec: /mnt/media_bulk/new-movies-H265/subdir 1/American Sniper.m4v
muru
  • 193,181
  • 53
  • 473
  • 722
SkiBum
  • 72
  • 8

0 Answers0