Solved

UNIX help please

Posted on 2009-05-04
3
294 Views
Last Modified: 2013-12-26
Im stuck on this problem. Im trying a nested while but I think Im going the wrong route.
Can anyone help me???  Thanks!
#!/bin/bash
 
echo "Author: Joe"
echo "Date: Saturday, May 02, 2009"
echo
echo "Displaying the content of a file from week 2."
echo
cat /mnt/sdb/week2/joe.3-7
echo
echo
echo "Please enter week number (1, 2 or 3) or q to quit."
read num
 
echo
 
((test=1))
while test $num != q
 
 do
 
  if [ $num -lt 4 ]
 
  then
 
  echo "The files submitted in week $num are:"
 
   ls ../week$num/joe*
 
  else
 
  echo "Incorrect week number!"
 
  fi
 
  ((file=1))
  while test $file != q
 
   do
 
   echo
   echo "Please enter file name to view the contents."
 
   read file
 
    if [ -f ../week$num/$file ]
 
    then
 
    echo
    echo "The contents of file $file are:"
 
    cat ../week$num/$file
    echo "The files submitted in week $num are:"
    ls ../week$num/joe*
   
    else 
    
    read -p "Incorrect file name! Please select again or enter q"
 
    fi
    ((file++))
   done
   ((test++))
 
echo
echo "Please enter week number (1, 2 or 3) or q to quit."
 
read num
echo
done

Open in new window

0
Comment
Question by:Cyber IT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:Cyber IT
ID: 24301180
This is what I have so far...

#!/bin/bash
# Author: Joe
# Date: 5.02.09

echo "Author: Joe"
echo "Date:" `date +%D`
echo
echo "Displaying the content of a file from week 2."
echo
cat /mnt/sdb/week2/joe.3-7
echo

#---------------------------------------------------------

echo
echo "Please enter week number (1, 2 or 3) or q to quit."
read num
echo

while [[ $num != q ]]
      do
            if [ $num -lt 4 ]
            then
                  echo "The files submitted in week $num are:"
                  ls ../week$num/joe*
            else
                  echo "Incorrect week number!"
            fi

while [[ $file != q ]]
      do
            echo
            echo "Enter the file name to view contents or enter q"
            read file
            if [ -f ../week$num/$file ]
            then
                  echo
                  echo "The contents of the file $file are:"
                  cat ../week$num/$file
            else
                  read -p "Incorrect file name! Please select again or enter q."
                  echo
                  file
            fi
      done
echo
echo "Please enter week number (1, 2 or 3) or q to quit."
read num
echo
done

When I enter 4 at the beginning it states wrong week but it still goes into the next while statement.  How do I get it to go back to the beginning till the correct number is entered??

After that it should go into the next while statement and when the name of file is entered it should show the content of file but then id like for it to either give me the option to enter another name or enter another number for week or quit.

Any help would be great!!

Thanks.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 25 total points
ID: 24301636
while [ $num -ge 4 ]
do
echo
echo "Please enter week number (1, 2 or 3) or q to quit."
read num
echo
if [ $num == q ]
then
   exit
fi

            if [ $num -ge 4 ]
            then
                  echo "Incorrect week number!"
            fi
done
0
 

Accepted Solution

by:
Cyber IT earned 0 total points
ID: 24357683
I ended up doing an "until" command to make it work instead of nesting the "while" command.

However as I look over your answer that would work too.

I dont know how I should score it?
0

Featured Post

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question