LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-14-2013, 02:34 PM   #46
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241

%H provides 24 hour time, but as a two digit number (01 to 24), rather than a 4 digit number.
 
Old 02-14-2013, 07:47 PM   #47
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
So just change my time values to 2 digits instead of the 4?
 
Old 02-14-2013, 09:23 PM   #48
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by druadunc91 View Post
So just change my time values to 2 digits instead of the 4?
Why don't you test it and see what happens? The nice thing about shell programming is you can test individual commands from the command line to see if they behave the way you expect. Had you run your "date +%H" command and looked at the output, even once, you would have seen that it spits out a 2 digit hour and your case statement would fail. 75% of learning a programming language is trying things to see if and how they work, and changing your syntax to see what effect it has. Coding something up blindly and then asking people to fix it when it doesn't do what you want it to do doesn't help anybody learn anything.

Last edited by suicidaleggroll; 02-14-2013 at 09:26 PM.
 
Old 02-25-2013, 05:30 PM   #49
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
can someone explain grep for me? i read about it and still dont really understand it.

also, this is some of my professor's code for the assignment

Code:
echo -e "$1\n$2\n$3\n$4\n$5" | sort | grep -v "^$"
what is the purpose of n$2, so on so forth and the grep code at the end?
 
Old 02-25-2013, 06:15 PM   #50
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Have you tried doing each part of the command?
Have you tried reading the manpage on "echo"?
How about reading the manpage on "bash"?

We aren't here to do your homework.
 
Old 02-25-2013, 09:01 PM   #51
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by druadunc91 View Post
can someone explain grep for me? i read about it and still dont really understand it.

also, this is some of my professor's code for the assignment

Code:
echo -e "$1\n$2\n$3\n$4\n$5" | sort | grep -v "^$"
what is the purpose of n$2, so on so forth and the grep code at the end?
Code:
man bash
man echo
man grep
4 pages in this thread and you still haven't learned how to look up anything for yourself?
 
Old 02-27-2013, 01:52 PM   #52
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
I understand you guys aren't here to do my homework. I got the task done with help from a friend in the class. But even using man pages, I still have a hard time understanding grep. I understand echo, when I read manpage for grep the explanation kind of flew over my head
 
Old 02-27-2013, 07:44 PM   #53
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
As per suicidaleggroll's post #48, have a quick read of the docs, then just try it. Its no effort to test this sort of stuff and much quicker than posting a qn.
If you are still stuck and have a specific qn, then ask.
http://catb.org/~esr/faqs/smart-questions.html
 
Old 03-15-2013, 11:27 AM   #54
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
I'm back just for help finding an error

here is my code

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

until [ -e $x ]
do
        echo -n "The file you have entered does not exist."
        echo
        echo -n "Please try to enter a file name and not gibberish: "
        read x

done

if [ -e $x ]

then
rm $x
echo "I have removed the file, bwhahahaha!!!"


        for [ $x = basename $x ]
        do
        echo "Numbnuts, I removed the program being executed."
        rm $x
        done

fi
this is the error I get
Quote:
./task06: line 22: syntax error near unexpected token `"$x"'
./task06: line 22: ` for [ "$x" = "basename $x" ]'
What is wrong here? I've been looking it at for a few hours and can't seem to find it. I know what I want the program to do. But the for loop is messing with me. the error is within the for loop around somewhere around it, because if I comment it out, my program runs the way it should so far.
 
Old 03-17-2013, 11:14 AM   #55
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Why are you using a "for" loop? Don't you want to use "if" (and you want '==', not '=', in your comparison expression)?

Note that since you're already calling "until [ -e $x ]" your next "if [ -e $x ]" is unnecessary.
 
Old 03-18-2013, 01:55 AM   #56
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
i tried changing my expression in my if statement but, when I change it, it doesn't remove the file after the user has input a valid file name.

It just quits the program after a valid file name is entered.

I changed it from what I have to this (i was just trying this):

Code:
if [ -e == $x ]
then
rm $x
echo "Bwhahaha, I just removed the file!!!"
fi
that is the code I used where it just exits out and nothing else happens.

But if I leave it as is, it works just fine with the until loop.
Code:
if [ -e $x ]
then
rm $x
echo "Bwhahaha, I just removed the file!!!"
fi
 
Old 03-18-2013, 02:04 AM   #57
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
I'm still getting the same problem with something I tried, but I know it will be easier. I moved the if statement to inside the until loop, but the if statement won't work the way I need it to work unless the expression is

Code:
if [ -e $x ]
i know its redundant to do this, but I can't seem to figure out basename
 
Old 03-18-2013, 02:05 AM   #58
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Some notes on the 2nd half of your program
Code:
#redundant due to the preceding 'until ... '
if [ -e $x ]

then
rm $x
echo "I have removed the file, bwhahahaha!!!"


        for [ $x = basename $x ]        # not a valid for loop construct eg http://www.cyberciti.biz/faq/bash-for-loop/
        do
        echo "Numbnuts, I removed the program being executed."
        rm $x              # pointless; you removed the file already above...
        done

fi
Incidentally, indent more like this
Code:
if [ -e $x ]
then
    rm $x
    echo "I have removed the file, bwhahahaha!!!"


    for [ $x = basename $x ]
    do
        echo "Numbnuts, I removed the program being executed."
        rm $x
    done

fi
ie between then ...fi and do...done

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

BTW, using 'set -xv' near the top of your prog will show what the parser is doping; very helpful for debugging.

Last edited by chrism01; 03-18-2013 at 02:07 AM.
 
Old 03-18-2013, 11:54 AM   #59
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
The construct "$x = basename $x" doesn't make sense...

First replace the leading $x with the value... you now have "file = basename $x", then replace the second one.. "file = basename file".

So if your file name is /tmp/file.extention then the result is "/tmp/file.extension = file.extension"

which is bogus.
 
Old 03-18-2013, 03:08 PM   #60
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
I know removing the file a second time is pointless, but that part is supposed to be if the user inputs the file that is being executed.

So I have one spot where it removes the file and says "Bwhahaha, I have removed the file!!!"

The second spot where I have rm $x is for if the user inputs the file being executed. So say you run the program, file, whatever you want to call it, if the user inputs the name of that file, it still removes it but also tells the user that the file that was removed was the one the user executed.
 
  


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 06:31 AM.

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