Solved

UNIX help please

Posted on 2009-05-04
3
285 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:-CyberIT-
  • 2
3 Comments
 

Author Comment

by:-CyberIT-
Comment Utility
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
Comment Utility
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:
-CyberIT- earned 0 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert OverPunch 25 57
Skill Development 15 122
Annoying "thing" blocks my view 4 49
unix example issues 18 44
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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now