LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Going LOOPY on a Friday - Script Help Plz (https://www.linuxquestions.org/questions/programming-9/going-loopy-on-a-friday-script-help-plz-750837/)

fusion1275 08-28-2009 06:10 AM

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

kbp 08-28-2009 06:47 AM

Seems a little homeworky :) ... what do you have so far ?

pixellany 08-28-2009 06:53 AM

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


JulianTosh 08-28-2009 06:57 AM

LOL

His pretext indicates he's been denied before...

kbp 08-28-2009 07:09 AM

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

fusion1275 08-28-2009 07:10 AM

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 :)

JulianTosh 08-28-2009 07:19 AM

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



All times are GMT -5. The time now is 02:14 PM.