Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Is it possible to set a variable for a few lines of echo ? After user enter a file name, the user will be asked to continue or not, if ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    8

    How to loop inside the case ?

    Is it possible to set a variable for a few lines of echo ?
    After user enter a file name, the user will be asked to continue or not, if user press enter with no input which is * option in the case, i want the user be re-asked the same case menu which is
    Code:
                    echo "You just entered file name: [$filename]"
    		echo "# Do you wish to enter another file name ?"
    		echo ""
    		echo "# Choices:-"
    		echo "# ---------"
    		echo "#    y/Y    -    Yes, I want to continue."
    		echo "#    n/N    -    No, show me the last file name I entered!"
    		echo -n "# Please enter your choice: "
    		read choice
    Any idea ?

    Full Code
    Code:
    status=yes
    while [ "$status" = yes ]
    do
    	echo -n "# Please enter file name: "
    	read filename
    
    	if [$filename = ""]
    	then
    		echo "# [Error] No Input!"
    		echo "# Please run this script again."
    		exit 1
    	else
    		echo "You just entered file name: [$filename]"
    		echo "# Do you wish to enter another file name ?"
    		echo ""
    		echo "# Choices:-"
    		echo "# ---------"
    		echo "#    y/Y    -    Yes, I want to continue."
    		echo "#    n/N    -    No, show me the last file name I entered!"
    		echo -n "# Please enter your choice: "
    		read choice 
    
    		case $choice in
    		y)	status=yes 
    			;;
    		n)	status=no
    			echo ""
    			echo "# Your last file name entered is : [$filename]" 
    			;;
    		Y)	status=yes 
    			;;
    		N)	status=no
    			echo ""
    			echo "# Your last file name entered is : [$filename]"
    			;;
    		*)	$repeat
    			;;
    		esac
    		shift
    	fi
    done
    Last edited by kahpeng; 10-06-2007 at 09:43 PM. Reason: Code Updated!

  2. #2
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    Is this homework/classwork?
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    8
    hi there ozar, i'm just doing exercise, try 2 improve n learn more in linux programming. am i asking silly questions to you ?

  4. #4
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    Quote Originally Posted by kahpeng View Post
    hi there ozar, i'm just doing exercise, try 2 improve n learn more in linux programming. am i asking silly questions to you ?
    No, it's against the forum rules to post homework/classwork questions:

    http://www.linuxforums.org/forum/lin...ums-rules.html
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Posts
    8
    ozar, sorry about that, i don't know. even if i post the code and ask some comment from others also can't ? i need someone to correct if i do wrong, i want to learn >.<

  6. #6
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    It's generally okay to post your code explaining what you've done to solve the problem yourself. People will often give clues and/or nudges to get you going.

    It's not okay to post questions looking for specific answers to homework/classwork questions.
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

  7. #7
    Just Joined!
    Join Date
    Oct 2007
    Posts
    8
    Quote Originally Posted by ozar View Post
    It's generally okay to post your code explaining what you've done to solve the problem yourself. People will often give clues and/or nudges to get you going.

    It's not okay to post questions looking for specific answers to homework/classwork questions.
    okay, I understand, maybe the way I post is not correct. So, this topic still consider "okay" or "not okay" ? Anyway, I changed my code while I was looking back at this topic, hope you don't close this topic as well. I added some error checking to my original code but need some advise to continue on.

    Please do let me know if I can continue post my modified code in this topic, thanks for explaining about what the rules. Maybe my 1st post was confusing to you, I don't mean to ask for answer, I wanted to know if there's any other solution other than awk, I did reading sed stuff & etc, you might think I'm posting question for answers only, sorry about that

    Cheers.

  8. #8
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    Yeah, you'll probably be okay as long as you don't ask for specific answers, or expect someone to provide the solutions for you.

    The forum rules are enforced at each moderator's discretion, so I can't speak for all mods.

    Have fun with Linux, and good luck with your Linux course.
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

  9. #9
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Quote Originally Posted by kahpeng View Post
    Is it possible to set a variable for a few lines of echo ?
    After user enter a file name, the user will be asked to continue or not, if user press enter with no input which is * option in the case, i want the user be re-asked the same case menu which is
    [...]
    May be something like this:

    Code:
    status=yes
    while [ "$status" = yes ]
    do
            [ "$invalid" ]||{ echo -n "# Please enter file name: "
            read filename;}
    
            if [ -z "$filename" ]
            then
                    echo "# [Error] No Input!"
                    echo "# Please run this script again."
                    exit 1
            else
                    [ "$invalid" ]||{
                    echo "You just entered file name: [$filename]"
                    echo "# Do you wish to enter another file name ?"
                    echo ""
                    echo "# Choices:-"
                    echo "# ---------"
                    echo "#    y/Y    -    Yes, I want to continue."
                    echo "#    n/N    -    No, show me the last file name I entered!";}
                    echo -n "# Please enter your choice: ";unset invalid
                    read choice 
    
                    case $choice in
                    [Yy])   status=yes 
                            ;;
                    [Nn])   status=no
                            echo ""
                            echo "# Your last file name entered is : [$filename]" 
                            ;;
                    *)      echo "# You have to make a choice.";invalid=y
                            ;;
                    esac
            fi
    done

  10. #10
    Just Joined!
    Join Date
    Oct 2007
    Posts
    8
    thanks radoulov, btw can i know -z purpose inside
    Code:
     if [ -z "$filename" ]
    the if statement ? Is it to check whether the variable is empty ?

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...