Problems executing an interactive shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Problems executing an interactive shell script
# 1  
Old 11-12-2016
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.



Code:
#!/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

# 2  
Old 11-12-2016
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:
Code:
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?
# 3  
Old 11-12-2016
Quote:
Originally Posted by Braveheart
[...]

Code:
#!/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

Perhaps, the following might be a guide:
Code:
#!/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


Last edited by Aia; 11-12-2016 at 03:00 PM.. Reason: s/extention/extension/g
This User Gave Thanks to Aia For This Post:
# 4  
Old 11-12-2016
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.

Code:
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.



Quote:
Originally Posted by Aia
Perhaps, the following might be a guide:
Code:
#!/bin/bash

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

---------- 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.

Last edited by Scrutinizer; 11-13-2016 at 05:36 AM.. Reason: code tags
# 5  
Old 11-12-2016
Quote:
Originally Posted by Braveheart
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remotely Executing Shell Script - Problems

Hey Lads, I have a shell script on a remote Server X that i need to execute from Server A. the script executes fine locally but remotely does not. It appears the script on the remote machine is calling another shell script which only has an array defined . Please see below the errors. ... (10 Replies)
Discussion started by: Irishboy24
10 Replies

2. UNIX and Linux Applications

How to write automated interactive shell script?

Hello everyone, I just want to write a shell script for automatic feeding the username and password prompts when running my commands, I tried this one but it did not work. Please help me for any way out. #!/bin/bash #!/usr/bin/expect cd ~/workspace/mimosanetworks_mimosa-nms ls -ltr ... (5 Replies)
Discussion started by: sandy-sm
5 Replies

3. Homework & Coursework Questions

Problems executing script

Hi, I'm a complete novice to Linux and UNIX. I'm having trouble getting a script to execute properly. I did a similar script earlier in the semester with no problems. For whatever reason I can't get this one to work. Any help would be greatly appreciated as I'm completely lost and frustrated at... (5 Replies)
Discussion started by: Lindy70
5 Replies

4. Shell Programming and Scripting

Shell script using an interactive command

Hello experts, I have a to write script for monitoring, the script would use a command and I plan to write the script as follows while true do command -arg sleep 2 clear done The output would be set up on a screen for monitoring. However the issue is that the command used in... (2 Replies)
Discussion started by: maverick_here
2 Replies

5. Shell Programming and Scripting

Use expect to run an interactive shell script?

Hi all, I have a bit of a vexing issue here and I'm not certain how best to go about it. Basically, I want to run a shell script and automate the user prompt of hitting 1 to fully uninstall Symantec Anti-Virus for OS X. Would expect be the best way to do this? (5 Replies)
Discussion started by: prometheon123
5 Replies

6. Homework & Coursework Questions

Help with Interactive / Non Interactive Shell script

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (1 Reply)
Discussion started by: rits
1 Replies

7. Shell Programming and Scripting

How to make interactive shell script a automated one?

Hi, I am new to shell scripting.I have written a very simple shell scipt that asks for the username and password on executing. i.e echo "Enter username :" read usrname; echol "Enter password :"; read passwd; echo usrname; echo passwd; but now I want to make it automatic , such... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

8. OS X (Apple)

interactive shell script to create users 10.4

Hello everyone, Not sure if this is the right place, but OS X isn't your standard Unix, so I figured here would be best. I am looking at creating a script that will be interactive that admins can run to create users. Now, 10.4 uses netinfo database and netinfo manager to handle it's users. ... (3 Replies)
Discussion started by: tlarkin
3 Replies

9. Shell Programming and Scripting

Schedule an interactive shell script

Hi, I need to schedule a shell script which executes another shell script along with a series of other commands. When the inner shell script is executed it prompts for a password..... This inner shell cannot be changed How can I do this???? Regards, Chaitrali. (4 Replies)
Discussion started by: Chaitrali
4 Replies

10. Shell Programming and Scripting

Korn shell interactive script

Hi, How can I prompt a user for two input and pass the input to variables in the script. I have the following script but it is not working: +++++++++Begin+++++++++++ #!/bin/sh database_c=$1 output_f=$2 echo "Your db is $1\nOutput is $2" +++++++++End+++++++++++ Thanks, Leonard (3 Replies)
Discussion started by: leonard905
3 Replies
Login or Register to Ask a Question