Problems executing an interactive shell script

I am new to Unix shell and to this forum.

I am having some trouble executing an interactive shell script that I have written using Mac TextEdit that takes a user input via terminal of a file type (jpg or gif) and then activates a script that will iterate through a folder of unsorted file types located on my desktop, with the aim being, to move these files to a new folder based on the users command.

i.e The user types jpg in the terminal and the script runs and moves all files of type jpg from unsorted into sorted

As it currently stands, When I run the code I am not getting an error, it's merely not moving these files from one directory to the other.

I have tried stack overflow and other sites without success. I do not know if the issue pertains to the fact that the user input is not being caught through the read command or perhaps there is a wider issue with my code.

Any help making this code run would be greatly appreciated.

#!/bin/bash

echo �Good Morning, Please enter your file type name for sorting [ENTER]:�
read file
if [[ �$file� == jpg ]]; then
       mv /Users/christopherdorman/desktop/unsorted/*.jpg /Users/christopherdorman/desktop/sorted
echo �Good News, Your files have been successfully processed� 
fi

Hi Braveheart,
Welcome to the UNIX & Linux Forums.

What operating system are you using?

What is the name of your script?

What exact command line do you type into your shell to run your script?

What output do you get when run your script?

What output do your get when you run the commands:

ls -l /Users/christopherdorman/[Dd]esktop/unsorted/*.jpg
ls -ld /Users/christopherdorman/[Dd]esktop /Users/christopherdorman/[Dd]esktop/sorted
ls -l script_name
od -bc script_name

where script_name (in both places where it is used above) is the name of your script?

Perhaps, the following might be a guide:

#!/bin/bash

echo "Good Morning, Please enter your file type name for sorting [ENTER]:"
read extension
mv  -v /Users/christopherdorman/desktop/unsorted/*.${extension} /Users/christopherdorman/desktop/sorted/
if [[ $? -eq 0 ]]; then
    echo "Good News, Your files have been successfully processed"
fi
1 Like

Thank you for the note Don.

In relation to your questions.

What operating system are you using? - OSX Yosemite 10.10.5

What is the name of your script? - My script name is scripts.txt that resides within scripts folder on my desktop

What exact command line do you type into your shell to run your script? - To execute my script don, I use ./scripts.txt

What output do you get when run your script? - I get no output, the destination folder does not populate with the jpg files.

What output do your get when you run the commands:

In terms of console/ terminal output.

ChristophersMBP:scripts christopherdorman$ ./scripts.txt
�Good Morning, Please enter your file type name for sorting [ENTER]:�
jpg
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory
ChristophersMBP:scripts christopherdorman$

---------- Post updated at 06:04 PM ---------- Previous update was at 05:29 PM ----------

Thank you very much. That worked wonderfully and I was almost going demented having asked the question on SO a few times without success.

May I asked, your successful code cited extention, is that the best way for the terminal to read the input for a script to run.

---------- Post updated at 06:06 PM ---------- Previous update was at 06:04 PM ----------

Thank you very much Aia. That worked wonderfully and I was almost going demented having asked the question on SO a few times without success.

May I asked, your successful code cited extention, is that the best way for the terminal to read the input for a script to run.

The command read requires one or more variables; it really doesn't matter what we name it, however it is a good idea to name it something meaningful. I committed an orthographic error. I meant extension. Which proves that it doesn't care what's named.
Of course, you want to use that after the variable is populated with information. That's done at /Users/christopherdorman/desktop/unsorted/*.${extension}

Try to avoid the word file as a variable. There is already a command to interrogate the system for file types and this command is called file