Solved

UNIX help please

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java ^ examples 8 62
NTP Server in VMware 5 159
Message not shown 5 29
Way to decrease size of apk file 9 40
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 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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

929 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

12 Experts available now in Live!

Get 1:1 Help Now