LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-05-2009, 03:09 PM   #1
froster
LQ Newbie
 
Registered: Sep 2006
Location: Central Illinois, USA
Distribution: Mint17, Mac 10.6, puppy5.5.3
Posts: 4

Rep: Reputation: 1
I cannot figure out how to get user input into a script


I have a section of a script that is supposed to change the filename of similar files in a directory. It works the way I need it to work, except that I would like to input the new filename each time it runs without having to edit the script.

I capture 5-6 images into ~/screenshots. They are labeled Picture 1.png, Picture 2.png, etc. I would like to be able to rename them as Lesson 1.png, Lesson 2.png, and so forth. It works if I hardcode the new NAME (Lesson) into the script, but I want to replace NAME with a variable that I can input at a prompt or pass into the script from another. I have tried setting a variable and replacing "Lesson" with $variable in line #11, but that leaves me with only the extension and no filename. I have also tried using $1 on the command line:

ie: $ ./namer $1
and fname=$1
and replace Lesson with $fname in line #11
but it still changes everything to a null filename

Here is what I have that works:

1 #!/bin/sh
2 # I need to get user input for new filename
3
4 # cd to screenshots directory
5 cd ~/screenshots
6
7 # this next line removes spaces from filenames
8 for f in *; do mv "$f" `echo $f | tr ' ' '_'`; done
9
10 #this changes the Picture to Lesson
11 ls * | awk '{print("mv -v "$1" "$1)}' | sed 's/Picture/Lesson/2' | /bin/sh


How do I get a variable into this script?

Thanks in advance!

JF
 
Old 11-05-2009, 03:13 PM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,038

Rep: Reputation: 97
Code:
read -p "Please enter new filename: " new_file
You will need to change the sed part of the script:
Code:
ls * | awk '{print("mv -v "$1" "$1)}' | sed "s/Picture/$new_file/2" | /bin/sh
You should be able to use $1, but you need to replace the single quotes in the sed statement into double quotes.

Last edited by Disillusionist; 11-05-2009 at 03:20 PM.
 
Old 11-05-2009, 08:13 PM   #3
froster
LQ Newbie
 
Registered: Sep 2006
Location: Central Illinois, USA
Distribution: Mint17, Mac 10.6, puppy5.5.3
Posts: 4

Original Poster
Rep: Reputation: 1
Thank you, Disillusionist! The double-quote was the fix I needed. Have a great day!

jf
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SHell Script Help! When reading input from a user sfmadmax Linux - Newbie 3 12-12-2007 01:03 AM
Shell script user input tuckermaddox Linux - Newbie 5 08-12-2004 04:14 AM
Masking user input in sh script? RMSe17 Linux - Software 3 07-20-2004 11:17 PM
mask user input in a bash script PlatinumRik Linux - Software 1 06-15-2004 11:06 AM
User input using a BASH script... causticmtl Programming 5 07-13-2003 10:59 PM


All times are GMT -5. The time now is 12:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration