Score:0

String Manipulation in a called bash script

cm flag

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 avatar
us flag
`sh script2.sh` != `./script2.sh`
SkiBum avatar
cm flag
Thank you! The similar post had the answer I needed.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.