Bash help
Hey there,
I'm new here and currently taking Bash Programming in college. I'm having a little problem right now with my code i'm trying to have my program output the date/time whenever a user inputs the word "time" when my program prompts them to enter something so far this is what the code looks like and I'm getting an integer expression expected Quote:
|
-eq is a numeric comparison.
You want either == (or = when used in an expression), also suggest space after the closing " on "time". Run down the bash manpage to "Conditional Expressions" |
that worked, thanks! I'll be back for more help soon, probably
question: with if statements, if you have an if inside an if, do you need a fi at the end of the inner if statement example: Quote:
|
Quote:
|
Ok,
so more problems. Here's my code: Quote:
Quote:
Quote:
|
And the question is?
|
Quote:
Code:
elif [ "$x" = "Directory" Code:
elif [ "$x" = "Directory" ] 2 of the 3 braces need spaces before the closing right bracket. time" ] and who" ] NOT time"] or who"] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS |
Quote:
|
Quote:
You might want to have a look at these: Bash resources: |
thanks for the help and info druuna!
on the who command, how do you get each user to appear on their own line so its not all mashed up? |
Quote:
Code:
elif [ "$x" == "Who" ] You may also want to start indenting your if blocks, it makes larger codes infinitely easier to read. |
added another elif statement, because my assignment seems to contain a lot of them. Anyways, all my scripts are working except for the last elif statement.
This is the error I get when I execute the script using the "trigger" work if you will. Quote:
Quote:
|
Quote:
|
That was my bad on typing here. My script has "". Still have same error
|
Please post the complete script, or at least the complete if-then-elif-fi part that doesn't work. Also post the exact error it throws.
|
Quote:
|
Quote:
|
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
1) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability. http://mywiki.wooledge.org/BashFAQ/031 http://mywiki.wooledge.org/ArithmeticExpression 2) When doing simple pattern matching on a variable string, a case statement is generally better to use. Unfortunately you can't do arithmetic comparisons in them (not directly, at least), but you can usually include a separate if statement either as a sub-command or as a separate one before or after it. 3) Finally, the shell has built-in variables for $PWD, $HOME, $USER and several others, so there's often no need for the external commands. See the bash man page. Code:
clear |
David,
thanks for the help. The reason I am using [] is because that is how my professor is teaching us right now. The help though is much appreciated and if I have more questions, I will keep posting. Thanks for all the help so far everyone! |
Do read the links given to you and please indent your code as shown above, its much easier to read that way.
Also, you can debug your code by using Code:
set -xv |
Code:
clear |
As above, prefer [[ ]] to [ ] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
|
Re post #21, That's not the way to properly indent your code. The idea is that all commands that occur at the same level of execution line up at the same level of indentation.
if, elif, else, and fi are all part of the same complex command enclosure, and so should line up, with the sub-commands that it executes under those conditions indented an extra level. In addition, many people prefer to put the then keyword on the same line as the if, since it's not an independent command, but paired with the previous keyword to bracket the test commands. It's how the shell knows where the test ends and the subcommands start. Putting them on the same line helps keep this visually clear and separate from the sub-commands (this can be relaxed if the test commands themselves occupy multiple lines). This suggestion also applies to the do keyword in for/while loops. Pay attention to vertical spacing too, which helps to visually define blocks that go together. And finally, get into the habit now of commenting your code clearly as you're writing. You'll be very thankful for them when you start reviewing old scripts you wrote and start wondering what the heck you were thinking when you wrote it! See here for more on good styling practice: Scripting With Style Code:
clear As I said before, a case statement would still be much better here, however, and the double-bracket tests. |
Code:
clear |
As per David the H.'s example, you should indent all cmds between then .. elif, not just other 'if' blocks.
Same for else .. fi. |
im back again. Some more help would be appreciated
For this next assignment, I'm taking my finished code and instead of using if's and elif's, I'm using case statements to make the program do exactly as my last one. this is what I have Code:
clear this is what I tried Code:
clear |
@druadunc91: To my knowledge the case statements expects a pattern to compare to and not a true/false expression.
Here is one way to edit your code to do what you require: Code:
# replace the following Also have a look here: ABSG - 11.4. Testing and Branching |
Is there a way to do that expression without the if statement? This assignment is using just case statements
|
Quote:
Code:
* ) |
As mentioned before, a case statement takes the input string and compares it to a list of globbing patterns. The first pattern that matches in the list has its commands executed. Each potential match is equivalent to a "[[ $var == *pattern* ]]" test.
The only way you could directly use a pattern match to calculate "more than/less than" would be something like this: Code:
case $text in An easier technique may be to match the length of the string directly, within a range of numbers: Code:
case ${#text} in In any case, druuna's example is certainly the way to go here. greater than/less than style matches really need bracket tests. But here again I would use an arithmetic evaluation instead. Code:
(( ${#var} > 20 )) && echo "Contains more than 20 characters" )) PS: your code is looking better. Keep up the good work! |
hey guys, im back again with more questions and help
here is my code: Code:
clear Thanks again EDIT: I took the %M out to look for just the hour and I'm still getting an error at line 18 and this is what the error says. Explanation of what is wrong would be greatly appreciated, i've encountered this error so many times and can seem to never find the problem Quote:
|
Using "date +%H%M" to determine the hour is not going to work - you are running both hour and minute into a single string. So the minimum value you get will be "100" (no minutes past 1AM). What you likely want is just %H (hours).
|
so just by taking out the %M it should fix at least that one part since i'm just looking for the time between hours to determine if it is either morning, afternoon, or evening?
|
That should do it.
|
i tried that and it didn't work...you can look at my previous post with the code in it. i added the error i'm getting from the code and bolded the line in which the error occurred. also, when i uncomment out the code that I'm having problems with, it causes the rest of my commands to not work when I test my program. And the error I'm getting is the same one on line 18 whether I type in Date to run the date command or Who for the who command
|
You can't use "case" with an 'if'-like structure: you used it correctly the first time (when you matched against e.g. "Directory", "Date") but for the second one you'll need to use if/elif/else instead.
|
Quote:
|
You could pattern-match, I guess:
Code:
case "$hour" in EDIT: Just seen that you use less than or equals, not less than - but seeing as this is a class assignment I'm sure you can figure out how to extend the examples I gave ;) |
Snark, thanks for the example. Now a question or two concerning your example. what does 1[7-9] mean? 7-9 is the pattern that the code is looking for and if it finds it, it would output evening and same thing for morning and afternoon? also, with your 1*) could I do 1[12-17] for afternoon, sticking with the military time that %H does?
|
Look at, say http://tldp.org/LDP/Bash-Beginners-G...ect_07_03.html.
1[7-9] should match 17, 18 and 19. 1* should match anything beginning with a '1' (which is why it is placed after 1[7-9]), but you could do 1[2-7] instead, yes (1[12-17] won't work). |
thanks for the help, its starting to click with me.
more questions. if there is no command line arguments, how would you get the program to output a specified command. example: you press return without typing anything in and the output is, say, the command who. how would you do that without getting too complex |
If I understand you correctly, match for an empty string and run
Code:
echo $(who) |
Do the same match, but drop the echo... Just use the who command directly.
The only time the $(...) syntax is useful is if you are putting the result into a variable. |
Quote:
|
thanks for all the help guys. more questions
here is my code. it all works, but im having a problem with one part. Code:
clear EDIT: I have it as %H for military time so I reflected that within the nested case statement |
%H provides 24 hour time, but as a two digit number (01 to 24), rather than a 4 digit number.
|
So just change my time values to 2 digits instead of the 4?
|
Quote:
|
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 "^$" |
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. |
All times are GMT -5. The time now is 11:22 AM. |