Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

UNIX help please

Posted on 2009-05-04
3
Medium Priority
?
297 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 85

Assisted Solution

by:ozo
ozo earned 100 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
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.:
Suggested Courses

885 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