Script that "should work" it's not working.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script that "should work" it's not working.
# 1  
Old 12-21-2010
Network Script that "should work" it's not working.

Hi, this is my script:
PHP Code:
#!/bin/bash
trabajo=$1
numero
=`jobs | grep -n $trabajo | cut -d':' -f1`
echo 
$trabajo
echo $numero
kill 
%$numero 
wait 
For some reason,
numero=`jobs | grep -n $trabajo | cut -d':' -f1`
that line wont save in numero the number I am looking for BUT if I write it in the bash...strangely, it works and saves the number.

What is wrong here? It's driving me crazy because if I type it manually it works flawless, however when executing the script it doesn't save the number in the variable.

Also, the echos are there just for testing purposes.
# 2  
Old 12-21-2010
That is because jobs are local to the shell. When you run a script you are creating a child shell and in this child the list of jobs are not inherited from the parent.

Try sourcing your script-
$ . my_script.bash

This will run the script in the current shell where the jobs are defined.
This User Gave Thanks to bisbell For This Post:
# 3  
Old 12-21-2010
Jobs will only list jobs started by the current shell. As your scipt is run in it's own version of the shell it won't should jobs started by the parent shell. You man need to use ps to do this (or run your script like bisbell describes above).

Also note that using -n on grep isn't very safe as your jobs might not be numbered from 1 (eg if you've run jobs in the past). best to use something like this:

Code:
numero=$(jobs | grep $trabajo)
numero=${numero#[}
numero=${numero%]*}

or like this if you don't mind using the sed external:
Code:
numero=$(jobs | grep $trabajo | sed 's/\[\([0-9]\)*\].*/\1/')


Last edited by Chubler_XL; 12-21-2010 at 06:37 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 12-21-2010
I want a script that kills the job, but sending the command

Example

./script sleep # this would kill the sleep job (if there is one).
# 5  
Old 12-21-2010
Perhaps something like this, and don't forget to run with $ . script_name

Code:
#!/bin/bash
trabajo=$1
numero=$(jobs | awk -F [][] "/$trabajo/"' {print $2; exit}')
if [ -z "$numero" ]
then
   echo No $trabajo Job found
else
    echo $numero - $trabajo
    kill %$numero
    wait %$numero
fi

Note above only kills the 1st job found - if you want to kill all jobs that match (*See my posting below*)

Last edited by Chubler_XL; 12-21-2010 at 07:26 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 12-21-2010
Wow, I sure am grateful for your help, thanks! I'm gonna try it out and study how it works, thank you!
# 7  
Old 12-21-2010
you're also making the assumption that the line number in the output of jobs is going to match the job number, which is not always true.

If you run the following commands, you'll see that job 3 is on the second line.

Code:
$ sleep 500 &
$ sleep 5 &
$ sleep 500 &
      wait 6 seconds, then
$ jobs
[1]-  Running                 sleep 500 &
[3]+  Running                 sleep 500 &

Try this:

my_script.bash
Code:
 #!/bin/bash
COMMAND=$*
JOB=`jobs | grep "$COMMAND" | sed 's/^\[\([0-9]*\)\].*/\1/'`
kill %$JOB

When you run it you'll have to source it in order to run it in the current shell and not a child.

$ . ./my_script.bash

Of course this does not take into account multiple jobs that contain the same text pattern. For that you'd have to create some kind of a loop.

Last edited by bisbell; 12-21-2010 at 07:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Beginners Questions & Answers

Automate "touch" bash script not working.

#!/bin/bash -i SAVEIFS=$IFS IFS=$"\n\b" picc=$* if ; then echo $TDATE if ; then touch dummy touch -t "tdate" dummy touch -r "dummy" "$picc" else echo -e "No mod date value to apply. If there is one in your shell,\ninvoke \eStarted asking advice on this (on Linuxquestions.org).... (9 Replies)
Discussion started by: iamwrong
9 Replies

3. Shell Programming and Scripting

Bash script: "mkdir -p" doesn't work with var(cat x)

Hello, :) I've an issue with the creation of a directory, All work without it :mad: So, below, my scripts with the debug output : #!/bin/bash # PATHS HOME_BACKUP="/home/backup" HOME_SCRIPT="/home/scripts/test/backup_server" TARGET="/var/www" # DATE DATE_Ymd=$(date +%Y-%m-%d) #... (1 Reply)
Discussion started by: Arnaudh78
1 Replies

4. Shell Programming and Scripting

awk script does not work when written as "one-liner"

In my quest to solve a bigger problem (See my previous post called "Create SQL DML insert statements from file using AWK or similar" - sorry, not allowed to post urls until I have > 5 posts) I'm trying to get my head round awk, but have some problem figuring out why the following script does work... (2 Replies)
Discussion started by: Yagi Uda
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

"sed" command is not working in shell script

Hi All, I am not much strong in shell scripting... I am using sed command in my script to find and replace a string....... This is how script looks : ############# #!/usr/bin/ksh CONFIG_FILE=iom_test.txt FIND=`echo "NIS_FTP_SERVER1=123.456.iom.com"` REPLACE=`echo... (2 Replies)
Discussion started by: askumarece
2 Replies

7. Shell Programming and Scripting

Script not working..."sort" not working properly....

Hello all, I have a file - 12.txt cat 12.txt =============================================== Number of executions = 2 Total execution time (sec.ms) = 0.009883 Number of executions = 8 Total execution time (sec.ms) = 0.001270 Number of... (23 Replies)
Discussion started by: Rahulpict
23 Replies

8. UNIX for Advanced & Expert Users

variables don't work for inline "su" script

My first post here... I cannot get variables to work when using inline KSH commands on a "su" command. I have a KSH script, below, that produces my problem: #!/usr/bin/ksh su <user_id> <<-END export FLD1=`echo $PWD` pwd echo $FLD1 echo TEST echo $PWD END The script will prompt me... (3 Replies)
Discussion started by: joekreif
3 Replies
Login or Register to Ask a Question