0

related to this thread answer
if the input is A B.py C D.py, the output turns into A.py B.py C.py D.py

I want to implement this feature in another script given below:

#!/bin/bash

echo -n "Round Name:"
read round
mkdir $round

read -r -p "Enter the filenames:" -a arr
for filenames in "${arr[@]}"; do 
cp ~/Documents/Library/Template.py $round/$filenames
done

How can i do that?

11
  • 3
    Where does $FileExt come from?
    – DonHolgo
    Jan 13, 2022 at 9:21
  • file extension i guess. I'm a noob at shell script
    – Mega Bang
    Jan 13, 2022 at 11:22
  • 1
    That's probably its meaning, but my question is how the variable is supposed to get a value, since it's not set in the script.
    – DonHolgo
    Jan 13, 2022 at 11:44
  • 1
    "tried this but didn't work" - What happened? What didn't happen that you expected to happen? What error messages did you get? Please remember that we are not sitting on your shoulder so you have to be precise in your description of a problem. Please edit your question to replace or expand on that sentence with detail. Jan 15, 2022 at 20:32
  • 1
    How should the script know that it needs to turn A into A.py and C into C.py? Is it simply a case of adding .py if there isn't already a .py on the end (so that either A or A.py are mapped to the same A.py)? Apr 1, 2022 at 12:47

1 Answer 1

2

It appears that what you want to do is to add a .py suffix to each argument unless there's one already present.

You can do this for a simple variable as in this example code

for item in A A.py
do
    dest=${item%.py}.py
    echo "Demonstrating that '$item' becomes '$dest'"
done

Here we're using a variable substitution to remove a trailing .py (if one is there to remove) and then always adding .py back on again. The single quotes inside the echo statement are not required.

Not the answer you're looking for? Browse other questions tagged .