Bash if run a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash if run a command
# 8  
Old 10-10-2014
Quote:
Originally Posted by cokedude
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.
We are not using different Linux distros. I am using a UNIX system where the output produced by the ipcs utility follows the formatting requirements specified by the standards.

If what you are trying to do is to remove message queues, shared memory segments, and semaphore sets that you own, the problem you stated in this thread seems to be useless. If you save the following script in $HOME/bin/cleanipc:
Code:
#!/bin/bash
# Usage: ipcs [-mqs] | cleanipc
#
# This script reads output from the ipcs utility from standard input and uses
# ipcrm to remove all message queues, shared memory segments, and semaphore
# sets whose owner is "bob".
echo ipcrm $(awk -v user="bob" '
$2 == "Shared" {
	type = " -m "
	next
}
$2 == "Message" {
	type = " -q "
	next
}
$2 == "Semaphore" {
	type = " -s "
	next
}
/^0x/ && $3 == user {
	o = o type $2
}
END {	print o
}')

and make it executable using:
Code:
chmod +x $HOME/bin/cleanipc

then the command:
Code:
ipcs -ms | cleanipc

it will show you an ipcrm command that will remove all shared memory segments and semaphore sets owned by "bob". If that looks like what you want to do, remove the echo shown in red in the script to have the script actually remove those IPC facilities.

Use different (or no options) on the ipcs command to process just message queues (-q), just shared memory segments (-m), just semaphore sets (-s), or everything (no options).

Note that the above script will NOT work on any system where the output produced by ipcs conforms to the standards. If your ipcs produced standard format output, the following code in the above awk script:
Code:
$2 == "Shared" {
        type = " -m "
        next
}
$2 == "Message" {
        type = " -q "
        next
}
$2 == "Semaphore" {
        type = " -s "
        next
}
/^0x/ && $3 == user {
        o = o type $2
}

would be replaced by:
Code:
$1 ~ /^(m|q|s)$/ && $5 == user {
        o = o " -"$1 $2
}

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 10-10-2014
Quote:
Originally Posted by Don Cragun
We are not using different Linux distros. I am using a UNIX system where the output produced by the ipcs utility follows the formatting requirements specified by the standards.

If what you are trying to do is to remove message queues, shared memory segments, and semaphore sets that you own, the problem you stated in this thread seems to be useless. If you save the following script in $HOME/bin/cleanipc:
Code:
#!/bin/bash
# Usage: ipcs [-mqs] | cleanipc
#
# This script reads output from the ipcs utility from standard input and uses
# ipcrm to remove all message queues, shared memory segments, and semaphore
# sets whose owner is "bob".
echo ipcrm $(awk -v user="bob" '
$2 == "Shared" {
    type = " -m "
    next
}
$2 == "Message" {
    type = " -q "
    next
}
$2 == "Semaphore" {
    type = " -s "
    next
}
/^0x/ && $3 == user {
    o = o type $2
}
END {    print o
}')

and make it executable using:
Code:
chmod +x $HOME/bin/cleanipc

then the command:
Code:
ipcs -ms | cleanipc

it will show you an ipcrm command that will remove all shared memory segments and semaphore sets owned by "bob". If that looks like what you want to do, remove the echo shown in red in the script to have the script actually remove those IPC facilities.

Use different (or no options) on the ipcs command to process just message queues (-q), just shared memory segments (-m), just semaphore sets (-s), or everything (no options).

Note that the above script will NOT work on any system where the output produced by ipcs conforms to the standards. If your ipcs produced standard format output, the following code in the above awk script:
Code:
$2 == "Shared" {
        type = " -m "
        next
}
$2 == "Message" {
        type = " -q "
        next
}
$2 == "Semaphore" {
        type = " -s "
        next
}
/^0x/ && $3 == user {
        o = o type $2
}

would be replaced by:
Code:
$1 ~ /^(m|q|s)$/ && $5 == user {
        o = o " -"$1 $2
}

Does my ipcs conform to standards? How do I know if it conforms to standards? The first method worked.

Is my understanding of this correct? After Shared is matched. Then it runs what is in {}. Then the next makes it read the next line? It ignore the Message block and Semaphore block until the column 2 is matched? It then goes to ^0x block? The process starts back over on the next line?
Code:
$2 == "Shared" {
    type = " -m "
    next
}

Can you please explain this part? I know you are anchoring the 0 to the beginning, then you making sure column 3 matches user which has been set to bob.
Code:
/^0x/ && $3 == user {
    o = o type $2

Did I do this right? I replaced everything in the single quotes ' '. It looked like you needed to keep the first part. I thought my ipcs conformed to standards but the first method worked.

Code:
#!/bin/bash
# Usage: ipcs [-mqs] | cleanipc
#
# This script reads output from the ipcs utility from standard input and uses
# ipcrm to remove all message queues, shared memory segments, and semaphore
# sets whose owner is "bob".
echo ipcrm $(awk -v user="bob" '
$1 ~ /^(m|q|s)$/ && $5 == user {
        o = o " -"$1 $2
}')

Thank you for your very detailed explanation. I wouldn't have remembered to use the bin directory.
# 10  
Old 10-10-2014
PS: If you use mmap()'d files not shared memory for these functions, you can kiss ipcrm and all that root stuff goodbye. Also no sudo needed, everyone can do shared memory in their own files, and the files are simple to dump after an abort or to monitor during a long run.
# 11  
Old 10-10-2014
No. Yes. Sure. And, no!

The output you showed us from the ipcs utility on your Fedora Linux distro does not conform to the standard in lots of ways. You need to keep the full script I gave you (with the exception of the echo) to get something that will work with the ipcs utility you have.

You said that the output from your ipcs looks like:
Code:
------ 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

so:
Code:
/^0x/ && $3 == user {
        o = o type $2
}

selects lines where the 1st two characters on the line are 0x and the 3rd field is bob (which I marked in red above). For each selected line it adds a string based on the section of the output from ipcs and the ID from that line to the string saved in the variable o.

If you had a conforming ipcs utility, the output for the last line in each section would have been:
Code:
m 3440645  0x00000000 666        bob        40         1                       
s 262146   0x000005b1 600        bob        6

instead of the output you showed us:
Code:
0x00000000 3440645    bob        666        40         1                           
0x000005b1 262146     bob        600        6

So, for other people reading this thread who do have a conforming ipcs, they could use:
Code:
#!/bin/bash
# Usage: ipcs [-mqs] | cleanipc
#
# This script reads output from the ipcs utility from standard input and uses
# ipcrm to remove all message queues, shared memory segments, and semaphore
# sets whose owner matches is the person who invoked this utility as determined
# by $USER from the environment.
ipcrm $(awk -v user="$USER" '
$1 ~ /^(m|q|s)$/ && $5 == user {
	o = o " -"$1 $2
}
END {	print o
}')


Last edited by Don Cragun; 10-11-2014 at 01:33 AM.. Reason: Fix typo.
This User Gave Thanks to Don Cragun For This Post:
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