Bash script gives weird output
Hello,
when executing the following bash script : Code:
echo "1" Quote:
Why are my Desktop files displayed ? |
I don't know ... but what does
Code:
/usr/bin/sipsak -vv -s sip:yo7@${SIPserver} -c sip:yo7@${SIPserver} --password 2aHOGrI Cheers, Tink |
Quote:
When executing this in bash script, it gives all the weird Desktop-information... |
You missed Tinkster's point :)
He wanted you to paste the exact output of the command. It's likely that your command's output includes an asterisk. Hence, when you: Code:
echo $CheckMainServer It may not be an asterisk, but some other character that is interpreted by the shell. I just used the asterisk as an example. EDIT: You can avoid some bash substitution problems by quoting variables. For instance: Code:
echo "$CheckMainServer" |
is this your hole script file? where is the shebang?
and please do tell what are you trying to do with this script. tanX |
Quote:
Thanks ! |
I have another question :
CheckMainServerOutput = "final received" When executing this : Code:
if [ "$CheckMainServerOutput" =~ "*ms*" ] || [ "$CheckMainServerOutput" =~ "*32.43.26*" ]; then Quote:
|
Quote:
|
Code:
# . : match any character Code:
if [[ "$CheckMainServerOutput" == *ms* ]] || [[ "$CheckMainServerOutput" == *32.43.26* ]]; then |
Quote:
No, it doesn't. Pattern matching uses =~, and uses an actual regex-engine, so to match a literal period .*32\.43\.26.* is the correct form. Try this: Code:
if [[ "$CheckMainServerOutput" =~ *ms* || "$CheckMainServerOutput" =~ *32.43.26* ]]; then Tink |
Quote:
On further reflection, the reason bash generated syntax errors on interpreting Code:
if [ "$CheckMainServerOutput" =~ "*ms*" ] || [ "$CheckMainServerOutput" =~ "*32.43.26*" ]; then |
Quote:
Code:
[[ "linux" == *in* ]] && echo ok |
Quote:
Whether the original string is what the OP really wanted I don't know. Quote:
Cheers, Tink |
Quote:
|
man bash:
Code:
... Code:
if [[ "$CheckMainServerOutput" == *ms* ]] || [[ "$CheckMainServerOutput" == *32.43.26* ]]; then |
Quote:
of bash's man-page that you quoted), which you claimed, by way of different example Quote:
Cheers, Tink |
Ok, I admit I wasn't clear :)
|
Hello,
so far I was helped very well with problems that occurred. Now I have a third question : Code:
echo "before" Quote:
Why does bash report "command not found" ? Is there something wrong with my syntax ? When I execute /sbin/service prog start on CLI, then output is normal : Quote:
|
When bash processes $StartProg=$(/sbin/service prog start), it:
|
But I read all over the internet that the syntax $(command) is the correct way of executing a command.
How do I then get normal output of the command so that the rest of the bashscript can continue ? |
Quote:
Re-read point #2 in catkin's message. In bash, when you want to use the value of a variable, you prefix the variable name with a '$'. Code:
echo "This is the value: $variableValue" Code:
variableValue="This is the assigned value" |
OK I see. I did not notice this before. In the rest of my script I did not make this mistake.
The error-message "=Starting: command not found" is gone, that's good ! However, even with StartProg=$(/sbin/service prog start) I still get no output and the script is not further executed. |
Quote:
The way to run a command is simply command This makes sense when you think of a bash script being a automated command prompt session. At the command prompt, you run a command by typing it and pressing Enter. |
Quote:
Code:
echo $StartProg |
Thank you for your help. I notice that execution of the command hangs... I think it is related to the service that is being started. That's why I now execute another bash script which executes this command. This way my "main" bash script is further executed. It works well.
Another question : suppose CheckNumber="[space][space][space]7" I want to get the last 2 ciphers of this string. If there is only one cipher, I want to delete white spaces. I do the following : Number=${CheckNumber:0:2} # get last 2 characters ${Number//[[:space:]]} # delete white spaces if [[ "$Number"=="0" ]]; then echo "number 0" else echo "number more than 0" fi I notice that the variable $Number is always empty. So my if-then-else also fails. See what I'm doing wrong ? |
If the command does not terminate you could put it in the background by appending & to the command.
|
Quote:
Bash wants spaces either side of comparison operators such as == and you want the numeric comparison operator -eq, not the string comparison operator ==. |
Code:
CheckNumber=" 7" |
The comment does not match the code in ...
Code:
Number=${CheckNumber:0:2} # get last 2 characters If you want to use echo to display the value of variables you must double quote them or bash will strip leading/training whitespace and convert all embedded whitespace sequences to a single space. It is also helpful to put markers at beginning and end so you can see any leading/training whitespace: Code:
echo "Number: '$Number'" Code:
if [[ "$Number"=="0" ]]; then |
${CheckNumber: -2} does now what I want it to do...
Number=${Number/ /} removes white spaces... However, still looking for a correct if-then-else |
The following is a good if-then-else :
if [[ $Number -eq 0 ]]; then |
Quote:
EDIT: actually ${Number/ /} removes only the first space. ${Number// /} removes all spaces and can be more simply given as ${Number// }. Here's a command prompt illustration: Code:
c@CW8:~$ x=' a b c ' |
All times are GMT -5. The time now is 10:38 AM. |