0

I'm new in bash programming and i like playing around a little bit. I'm trying to execute this script

#!/bin/bash

echo "Hello there, i am a little script so you can change a number of txt file 

names at once"

        read -p "I would like to know your name before we start, what is your name? " NAME

echo "Well hello there $NAME, nice to meet you im Scripty"

echo "Let's start"


read -p "What type of file would you like to rename(txt,sh,etc...)" ANSWER

        case "ANSWER" in
          [tT] | [tT][xX][tT])

        echo "You selected a TXT file type,Good job $NAME"
        ;;
          [sS] | [sS][hH])

        echo "You selected a SH file type,Good job $NAME"

FILES=$(ls *.$ANSWER)

read -p "To what name you would like to change the file name?" NEW

for FILE in $FILES
  do 
    echo "Renaming $FILE to new-$FILE"
    mv $FILE $NEW-$FILE
  done  

And this is the output i get

root@ubuntu:/home/daniel# ./change_the_name_of_the_file.sh 
Hello there, i am a little script so you can change a number of txt file names at once
I would like to know your name before we start, what is your name? Daniel 
Well hello there Daniel, nice to meet you im Scripty
Let's start
What type of file would you like to rename(txt,sh,etc...)txt
./change_the_name_of_the_file.sh: line 33: syntax error: unexpected end of file

And i cant figure it out to save my own life.

Would really like some help in this.

Thank you in advance

  • 1
    Nice :-) Reminds me of my first steps. You forgot to close the case statement with an esac (=case backwards) statement. – PerlDuck Jul 9 at 12:38
  • 1
    ... there are a number of other issues ex. "ANSWER" is a literal string (you need $ANSWER although you should avoid using ALLCAPS variable names in your scripts) and also you should replace FILES=$(ls *.$ANSWER) by FILES=(*.$ANSWER) and then loop over the array like for FILE in "${FILES[@]}" for the reasons discussed here: Bash Pitfall #1 – steeldriver Jul 9 at 12:45
  • Thank you all very much! you helped me to figure it out! esac closed! – Daniel Jul 9 at 13:06
1

I fixed some issues in the script and added comments inline. See below:

#!/usr/bin/env bash

echo "Hello there, i am a little script so you can change a number of txt file names at once"

read -p "I would like to know your name before we start, what is your name? " NAME

echo "Well hello there $NAME, nice to meet you im Scripty"
echo "Let's start"

read -p "What type of file would you like to rename(txt,sh,etc...)" ANSWER

# ANSWER is a variable whose value must be accesses as $ANSWER.
# To avoid weird errors for ANSWERs with spaces in it, we put it in "double quotes":
case "$ANSWER" in
    [tT] | [tT][xX][tT])
        echo "You selected a TXT file type,Good job $NAME"
        ;;
    [sS] | [sS][hH])
        echo "You selected a SH file type,Good job $NAME"
        ;;

    # If none of the above applies it's always good to have a default case:
    *)
        echo "You picked something else. Not sure whether I can handle that..."
        ;;
# close/end the opening "case" statement:        
esac

# The original
#    FILES=$(ls *.$ANSWER)
# parses the output of the `ls` command. This is ok for one-shot
# commands if you know for sure that your filenames do not contain strange
# characters. But the proper way is to use _globbing_ and assign
# the filenames to a bash array:
FILES=(*.$ANSWER)

read -p "To what name you would like to change the file name?" NEW

# Iterating an array needs some special syntax:
for FILE in "${FILES[@]}"
  do 
    echo "Renaming $FILE to $NEW-$FILE"
    # Again "double quotes" because the filenames may contain spaces.
    # I prepended the line with "echo ..." so it does not do any harm.
    echo mv "$FILE" "$NEW-$FILE"
  done  
  • WOW thank you very much it helped me do understand the problem i had – Daniel Jul 9 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.