Bash if run a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash if run a command
# 1  
Old 10-09-2014
Bash if run a command

I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string.

This works perfectly.

Code:
echo $(ipcs | grep Shared | awk '{print $2}')

When I put it in an if statement I get some problems.

Code:
$ if [ "$(ipcs | grep Shared | awk '{print $2}')" -eq "Shared"]; then                echo expression evaluated as true;             else                echo expression evaluated as false;             fi
bash: [: missing `]'
expression evaluated as false

$ if [ "$(ipcs | grep Shared | awk '{print $2}')" = "Shared"]; then                echo expression evaluated as true;             else                echo expression evaluated as false;             fi
bash: [: missing `]'
expression evaluated as false

$ if [ "$(ipcs | grep Shared | awk '{print $2}')" == "Shared"]; then                echo expression evaluated as true;             else                echo expression evaluated as false;             fi
bash: [: missing `]'
expression evaluated as false

I tried ==, =, and -eq because I wasn't sure which one to use.
# 2  
Old 10-09-2014
Hi,

Need to enclose in single quotes like so;

Code:
if [ `$(ipcs | grep Shared | awk '{print $2}')` == "Shared"]

Regards

Dave

Last edited by gull04; 10-09-2014 at 07:31 AM.. Reason: Error - Cut and paste
# 3  
Old 10-09-2014
You need a space after the [ and before the closing ]
Code:
if [ "$(ipcs | grep Shared | awk '{print $2}')" = "Shared" ]; then

Shorter is
Code:
if [ "$(ipcs | awk '/Shared/ {print $2}')" = "Shared" ]; then

Both are not portable.
# 4  
Old 10-09-2014
Quote:
Originally Posted by gull04
Hi,

Need to enclose in single quotes like so;

Code:
if [ `$(ipcs | grep Shared | awk '{print $2}')` == "Shared"]

Regards

Dave
Those look more backticks to me `. On my system single quotes aren't slanted '. Are backticks better than double quotes?

Quote:
Originally Posted by MadeInGermany
You need a space after the [ and before the closing ]
Code:
if [ "$(ipcs | grep Shared | awk '{print $2}')" = "Shared" ]; then

Shorter is
Code:
if [ "$(ipcs | awk '/Shared/ {print $2}')" = "Shared" ]; then

Both are not portable.
That worked Smilie. Can you please explain why you need spaces at the beginning and end [ ] brackets for if statements? I always forget when I don't do shellscripting for awhile.

I sometimes forget how awesome awk is Smilie.

What do you mean by not portable? Is it not posix compatible? Is there a way to make it portable?
# 5  
Old 10-09-2014
I don't understand what you're trying to do. On a system with no active message queues, shared memory segments, or semaphores, the output from ipcs will be something like:
Code:
iIPC status from <running system> as of Thu Oct  9 11:22:09 PDT 2014
T     ID     KEY        MODE       OWNER    GROUP
Message Queues:

T     ID     KEY        MODE       OWNER    GROUP
Shared Memory:

T     ID     KEY        MODE       OWNER    GROUP
Semaphores:

So, the output from:
Code:
ipcs|grep Shared|awk '{print $2}'

will be:
Code:
Memory:

and the condition in your if statement will always evaluate to false.

If, in addition to showing us code that doesn't work, you would also tell us what you're trying to do, we might be better able to help.

And, no. You do not want to enclose a command substitution in backquotes unless you are trying to perform a command substitution on the results of a command substitution.
# 6  
Old 10-10-2014
Quote:
Originally Posted by Don Cragun
I don't understand what you're trying to do. On a system with no active message queues, shared memory segments, or semaphores, the output from ipcs will be something like:
Code:
iIPC status from <running system> as of Thu Oct  9 11:22:09 PDT 2014
T     ID     KEY        MODE       OWNER    GROUP
Message Queues:

T     ID     KEY        MODE       OWNER    GROUP
Shared Memory:

T     ID     KEY        MODE       OWNER    GROUP
Semaphores:

So, the output from:
Code:
ipcs|grep Shared|awk '{print $2}'

will be:
Code:
Memory:

and the condition in your if statement will always evaluate to false.

If, in addition to showing us code that doesn't work, you would also tell us what you're trying to do, we might be better able to help.

And, no. You do not want to enclose a command substitution in backquotes unless you are trying to perform a command substitution on the results of a command substitution.
It looks like we are using two different distros of Linux. Our output is a bit different. I am using Fedora.

Code:
$ ipcs

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 262145     bob        600        393216     2          dest         
0x00000000 2523138    bob        600        393216     2          dest         
0x00000000 2555907    bob        600        393216     2          dest         
0x00000000 3375108    bob        600        998400     2          dest         
0x00000000 3440645    bob        666        40         1                       

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     
0x000005b1 262146     bob        600        6         

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages

I am working on a C project that spawns threads, creates shared memory, and creates semaphore arrays. It is annoying to have to keep typing or searching my bash history for:

Code:
ipcrm -s $(ipcs | grep bob | awk '{printf "%s ",$2}')
ipcrm  shm $(ipcs | grep bob | awk '{printf "%s ",$2}')

So I was thinking I could do something like this.

Code:
if [ `$(ipcs | grep Shared | awk '{print $2}')` == "Shared"]
ipcrm  shm $(ipcs | grep bob | awk '{printf "%s ",$2}')

I wanna do that first behavior until $2 equals Semaphore.

Code:
if [ `$(ipcs | grep Semaphore | awk '{print $2}')` == "Semaphore"]
ipcrm -s $(ipcs | grep bob | awk '{printf "%s ",$2}'

)


I am not sure how to this. I have been reading the bash documentation for several days with no luck.
# 7  
Old 10-10-2014
You had several options given to you in your duplicate thread, and it's not clear whether you tried any of them. Particularly, my suggestion with the while loop and printing the options to help figure out why it's not matching when it apparently should.

Please try that, and get back to me, otherwise I'm just wild guessing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compilation error when I run Bash configuration command

Hi, I downloaded source code file from The GNU website and changed the source code of ls.c file, added printf command to it. It worked fine. Then, I deleted the printf command, saved the file and ran the command 'make sudo && make install' closed the terminal and printf statement went away. I... (1 Reply)
Discussion started by: akanksha1509
1 Replies

2. Shell Programming and Scripting

Run command through html+cgi in bash

Hi everyone, I want to kill process through the web, so I create html page with single bottom that run kill command in shell script with CGI. Here is html code: <td><form METHOD="GET" action="http://IP:port/cgi_bin/script.cgi" > <input type="submit" value= "Submit" > <INPUT name="q"... (7 Replies)
Discussion started by: indeed_1
7 Replies

3. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

4. Shell Programming and Scripting

Bash code will not run

Why doesn't the code below run? Am I missing something? Thank you :). syntax() { printf "\n\n" printf "Enter HGVS description of variant(s): "; IFS="," read -a hgvs && printf "\n Nothijng entered. Leaving match function." && sleep 2 && return for ((i=0;... (5 Replies)
Discussion started by: cmccabe
5 Replies

5. Shell Programming and Scripting

Run bash command inside zsh script

Hi, I would like to run following code in bash inside a zsh script. (In this case is output unfortunately very different if you run it in zsh). I tried to put "bash" in front of the code but I obtained following error message "bash: do: No such file or directory " eve though I merged the whole... (7 Replies)
Discussion started by: kamcamonty
7 Replies

6. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

7. Shell Programming and Scripting

Bash- Command run from script does not pass full parameters with spaces inside

There's a JavaScript file that I call from command line (there's a framework) like so: ./RunDiag.js param1:'string one here' param2:'string two here' I have a shell script where I invoke the above command. I can run it in a script as simple as this #!/bin/bash stuff="./RunDiag.js... (4 Replies)
Discussion started by: AcerAspirant
4 Replies

8. Shell Programming and Scripting

ssh to run bash script

I want to use ssh to start a bash script that I have uploaded to a webhost. How do I do that from linux? (2 Replies)
Discussion started by: locoroco
2 Replies

9. Shell Programming and Scripting

Run bash script from webhost

If I have a script like this: while true do wget www.***.com >> file sleep 3600 done Is it possible to upload it to a webhost and have it run indefinitely. I have a hostgator account. How do I do this? (1 Reply)
Discussion started by: locoroco
1 Replies

10. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies
Login or Register to Ask a Question