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
Solved

UNIX help please

Posted on 2009-05-04
3
289 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
  • 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ShiftLeft challenge 21 84
abstract class with all non abstract mentods 6 79
unix in java example 9 70
Bartender label printing - switch on and off graphics 3 54
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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 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…
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.

809 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