Syntax Killer Syntax Killer - 26 days ago 7
Linux Question

catching user input to run scripts

I am trying to write a small bash script using mac OS TextEdit that will catch a user input in terminal and based on the file type stipulated in the command line albeit (jpg or gif) iterate through a directory on my desktop named

dir1
, pull all files of that filetype and place such in a new directory called
dir2


i.e The user types jpg into the terminal and the script kicks into life and pulls all of the jpg files situated in
dir1
and places such in
dir2


What is the leanest and least convoluted way of achieving this conscious that I am new to shell scripting.

I am about to reach for the meds. What can I do to the below code to get it to work.

#!/bin/bash

echo “Good Morning, Please enter your file type for sorting [ENTER]:”
read $FILE
if [[ $file == *.jpg ]]; then
mv ~/DIR1/*jpg* ~/Users/christopherdorman/desktop/dir2/
echo “your files have been successfully processed”
fi

Answer

There are a couple confusions here regarding bash variables and syntax. You need to use fi to close your if statement instead of done. Also, you need to capitalize the variable in your if statement, since bash is case sensitive. I believe this is what you are looking for (assuming your input is "jpg" or "gif"):

#!/bin/bash

echo “Good Morning, Please enter your file type for sorting [ENTER]:”
read FILE
if [[ $FILE == "jpg" ]]; then
    mv ~/DIR1/*jpg* ~/Users/christopherdorman/desktop/dir2/
    echo “your files have been successfully processed”
fi