Using A Variable In A Script

Good day,

I have the following script as an example...

# CREATE NECESSARY SERIES DIRECTORIES
mkdir -p /mnt/user/"Television- Media Server"/"Looney Tunes (1929)"

# RECREATE MISSING SERIES METADATA
cp /mnt/user/"Television- All Media"/"Looney Tunes (1929)"/* /mnt/user/"Television- Media Server"/"Looney Tunes (1929)"/

# ADD CONTENT TO PRODUCTION SERVER
cp -R /mnt/user/"Television- All Media"/"Looney Tunes (1929)"/"Season XXXX" /mnt/user/"Television- Media Server"/"Looney Tunes (1929)"

So, the challenge...

I need to modify this script to set a variable between "1929" and "1969", and substitute that number for the season number in the copying commands. All the documentation I found on this is confusing, so any help would be greatly appreciated.

I can use this to generate the number...

season=$((RANDOM % (1969 - 1929 + 1) + 1929))

My confusion is the proper format to substitute "season" for the "XXXX" portion in the copy command above? I'd appreciate any help you could provide...

Thanks!

1 Like

You can have "string1/string2" instead of concatenation "string1"/"string2".
A $ prefix returns the value of something.
$season is substituted inside a "string". Delimit the variable name as ${season} in case characters follow that otherwise could belong to the variable name.

season=$((RANDOM % (1969 - 1929 + 1) + 1929))
...
cp -R "/mnt/user/Television- All Media/Looney Tunes (1929)/Season ${season}" "/mnt/user/Television- Media Server/Looney Tunes (1929)"

Likewise $RANDOM returns the value of RANDOM.
echo "$RANDOM"
The $(( )) enforces a number expression, and RANDOM is not a number so must be a variable; the $ prefix can be omitted here.
echo "$(($RANDOM))"
echo "$((RANDOM))"

1 Like