LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-28-2009, 06:10 AM   #1
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Mint 13
Posts: 265

Rep: Reputation: 35
Going LOOPY on a Friday - Script Help Plz


Hi all,

I seem to of forgotten all basic shell scripting today and cant kickstart my brain so I am asking for some help.

Here is what I am attempting to do:

Quote:
1) Script asks for how many files to process (user input)
2) Script stores number in a variable
3) Scripts asks for file names until the count is reached
4) User enters file names on cmd line
5) Script reads users inputs
6) Scripts stores file name in variables
7) Script then processes each file separately
So basically it would be something like this:

Quote:
Please enter the number of files:
(stores the number)
Please enter the file name here:
(Stores the name and continues to asks the same question until the value is finished. Once all has been entered it will continue with my other piece of code)
Any help please???
 
Old 08-28-2009, 06:47 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Seems a little homeworky ... what do you have so far ?
 
Old 08-28-2009, 06:53 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
What is the context of this? The way you describe it, it looks like a cut and paste homework question, but your posting history suggests not.

The basic drill involves the "read" command. Try this in a terminal:

Code:
while true; do read -p "enter word: " word; echo "you entered $word"; if [ $word == stop ]; then break; fi; done
This should give you a pretty good idea of the basic approach.

To write it in a script:
Code:
while true; do
    read -p "enter word: " myword
    echo "you entered $myword"
    if [ $myword == stop ]; then
        break
    fi
done
 
Old 08-28-2009, 06:57 AM   #4
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
LOL

His pretext indicates he's been denied before...
 
Old 08-28-2009, 07:09 AM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Ah well... here's my 10 minute work of brilliance:

------------------------------------------------
#!/bin/bash

echo -n "How many files would you like to process? "
read COUNT

for instance in $(seq 1 1 $COUNT)
do
echo -n "File ($instance) name: "
read names[$instance]
done

for instance in $(seq 1 1 $COUNT)
do
echo "Processing file ${names[$instance]} ..."
echo "doing shite here..."
done
------------------------------------------------

cheers,

kbp
 
Old 08-28-2009, 07:10 AM   #6
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Mint 13
Posts: 265

Original Poster
Rep: Reputation: 35
Reading it back, it does sound like a homeworky type question doesnt it! LOL

To be honest it's me not being able to think today (brain shut down at the end of a hard week) and I just thought you chaps could put me in the right direction.

If it violates any of your "helping" rules then I will just tackle it next week. Its cool
 
Old 08-28-2009, 07:19 AM   #7
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
To affirm my brain cells...

Good job brain cells! Good job!

Code:
#!/bin/bash

echo -n "How many files? "
read count

while [ $count -gt 0 ];
do
  count=$((count-1))
  echo -n "Filename: "
  read files[count]
done

#do your stuff here
for i in "${files[@]}";
do
 echo $i
done
 
  


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
loopy skippy sound fixes itself with reboot from win2k randomsel Slackware 3 10-03-2007 06:30 PM
Something Loopy in Assigning Strings MattFunke Programming 3 09-19-2007 02:26 AM
Loopy mouse pointer tamtam Linux - Laptop and Netbook 3 09-15-2006 06:34 AM
loopy aiptek 6000 tablet netsurf Linux - Hardware 0 04-23-2005 05:58 PM
howto start a perl script only from tuesday to friday at 8.00 am ? cccc Linux - Newbie 4 03-19-2005 04:23 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration