LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-18-2013, 09:00 PM   #61
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325

You still don't need to remove it again.... that section is redundant; just test the filename and put the msg out.
 
Old 04-04-2013, 05:49 PM   #62
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
so far, I've made a lot of progress with my current program by myself and with a friend's help. anyways, a new problem is frustrating me.

here is my code:

Code:
a=`basename $0`

echo -n "Please enter a valid file name:  "
read x

until [ -e $x ] && [ $x != "$a" ]
do
        if [ $x == $a ]
          then
          echo "Leave this file alone!  Why can't you just leave this file alone!"
          read x

                else
                  echo "Thats not a file, try again!"
                read x
        fi
done

rma $x
echo "Whoops, there goes the file, right out the window."
echo "Would you like to re-create it? (Y/N)"
read choice
while [ $choice != Y ] && [ $choice != y ] && [ $choice != N ] && [ $choice != n ]
do
        echo "You're a smart one. This is a Y or N question."
        read choice
done

if [ $choice = Y ] || [ $choice = y ]
then
        for i in {1..1000}
        do
           echo "Enter something or QUIT"
           echo -n "$i. "
                read line

        if [ $line = "QUIT" ]
        then
            break
        fi

echo $line >> $x
echo "Added."
 done

wc=`wc -l $x | awk '{print $1}'`

echo "Hey you have" $wc "lines."

fi

exit
my problem is one of the requirements is even if the user types in the file being executed, for example, the name of this program is task06, so if the user typed in task06, it is supposed to still remove it but say something "smart" if you will. Because the way my professor has the requirement worded, the customer is always right.

Question #1: How do I get the program to exit the first until loop after it sees the file that was entered is the one being executed and go into the next part and let the user rebuild the file?

Question #2: When I execute my program to test it, if I type in more than one thing, including spaces, when the rebuild option comes up, it gives me the "too many arguments" error or "expecting unary operator expected". It still writes to the file and the lines that are added are shown in that file. And the program continues as it should, even with the errors. How do I fix the errors?

Other than these two questions, I think I'm fine at this point.

Oh, and by the way, to so no searching has to be done for the errors, they are on line 39 where I have if [ $line = "QUIT" ]
 
Old 04-04-2013, 07:47 PM   #63
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Some general pts

1. always start the fiel with the shell you want eg
Code:
#!/bin/bash
2. to debug, after the shell (hash-bang) line add set -xv
Code:
#!/bin/bash
set -xv
3. for your sanity and ours and your Lecturer's, stop using 1 char var names like x, a etc.
Get into the habit right now of using proper var names that mean something

4.
Code:
rma $x
'rma' ???

5. Use double brackets, not single http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
See previous mention in post #7
In fact you've been given several links to guides/tutorials; you must read these, not just ignore them.

Don't use cmd names as var names (wc).

Try adding some comments to your code; its getting long enough to justify it.

If you've got syntax/alogorithm issues, don't fiddle with the entire program, put minimal code into a test script and play with that until you get it straight, then put working version back in real script.

6.
Quote:
Oh, and by the way, to so no searching has to be done for the errors, they are on line 39 where I have if [ $line = "QUIT" ]
Can you re-write this more clearly eg 'to so no' ??

Last edited by chrism01; 04-05-2013 at 03:07 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Share And Discover Cool Bash Tricks With Bash One-Liners LXer Syndicated Linux News 0 01-30-2012 10:50 AM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 06:52 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 02:14 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 07:42 PM


All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration