What's wrong with my bash code?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What's wrong with my bash code?
# 1  
Old 11-08-2013
What's wrong with my bash code?

I want to let sleep 3 in the background and echo $i

Code:
pkglists="a b c d e f g"
f()
{
local i
set -- $pkglists 
[ ! -z "$1" ] && ((i +=2)) && sleep 3 &;echo $i
}
f

# 2  
Old 11-08-2013
This
Code:
[ ! -z "$1" ] && ((i +=2)) && sleep 3  &

gets executed in a new process in the background so a new $i get incremented in the background, but the $i that is local to the function does not get incremented in the foreground and therefore remains in its unitialized state.
# 3  
Old 11-08-2013
Quote:
Originally Posted by Scrutinizer
This
Code:
[ ! -z "$1" ] && ((i +=2)) && sleep 3  &

gets executed in a new process in the background so a new $i get incremented in the background, but the $i that is local to the function does not get incremented in the foreground and therefore remains in its unitialized state.
so how should I get it increased,

actually I want to get the pid of sleep 3

Code:
[ ! -z "$1" ] && ((i +=2)) && sleep 3 &

I want to get it's pid and put it in a lists something like
pid[i]= $!

how could I do
# 4  
Old 11-08-2013
read your shell's man page, here bash:
Quote:
Special Parameters
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
.
.
.
! Expands to the process ID of the most recently executed background (asynchronous) command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash conditional | getting logic wrong?

I have a file cat <<EOF > /tmp/test Line one Line two Line three EOF Now I am trying to get the exit stat ($?) of 1 if any text is found and 0 if any text is not found using grep, i.e. just reversing the exit status of grep # (snippet 1) this one is not working!!! retval $?... (4 Replies)
Discussion started by: the_gripmaster
4 Replies

2. Shell Programming and Scripting

What is wrong with my code?

Hello, all Suppose my current directory has 3 files: file_1 file_2 file_3 I wrote the following codes: awk 'BEGIN{while("ls"|getline d) {myarray++}}; END{close("ls");for (i in myarray){print i, myarray}}' /dev/null I expect the output be like: 1 file_1 2 file_2 3 file_3 ... (7 Replies)
Discussion started by: littlewenwen
7 Replies

3. Shell Programming and Scripting

What's wrong with this code?

Trying to do a file count on files between a specific date. I entered the following command, but it's not working: find . -type f \( -newer startdate -a ! -newer enddate \) -exec "ls -l | wc -l" {} \; lil help? :D (4 Replies)
Discussion started by: bbbngowc
4 Replies

4. Shell Programming and Scripting

whats wrong in my code

Code: #!/usr/bin/perl -w use strict; use warnings; #Clears Screen $CLEAR=`clear`; print $CLEAR; i get the below error: Global symbol "$CLEAR" requires explicit package name at ./mutmg.pl line 6. Global symbol "$CLEAR" requires explicit package name at ./mutmg.pl line 7. (1 Reply)
Discussion started by: sophos
1 Replies

5. Shell Programming and Scripting

Please help to fingure out what wrong with my tomcat restarting bash script

Hi, I am a nbee to Unix, I have used following script to check my tomcat is running or not and restart if it down. but actually it restart my tomcat each time running even my tomcat still running fine: Script that can run a check and perform an action if the check fails ... (1 Reply)
Discussion started by: quyennd
1 Replies

6. Shell Programming and Scripting

What is wrong with this code

I just wanted to assign the filename to a variable filename="abc" datestrng=`date +%Y%m%d` filextn="txt" "LOCAL_FILE"${i}=${filename}"_"${datestrng}"."${filextn} echo "LOCAL_FILE"${i} I get the following error on 2nd last line ksh: LOCAL_FILE1=abc_20081114.txt: not... (3 Replies)
Discussion started by: mqasim
3 Replies

7. Shell Programming and Scripting

what's wrong with my bash script?

hi, please point out what's wrong with my script. im feeding it a list containing fqdn, sit should ssh into each and verify that atleast one of its virtual backup ip resolves into one of its virtual hostnames .. anyway the objective shows in the script... however, im having problems in the ... (4 Replies)
Discussion started by: ikk
4 Replies

8. Shell Programming and Scripting

What's wrong with this code?

Hello all, Can someone tell me why I'm getting an error in the following code: export return_code="$?" if then echo "load_shaw.sas failed." exit else echo "Trigger the next script..." # /path/to/next/script fi I get an error... (3 Replies)
Discussion started by: mmignot
3 Replies

9. Shell Programming and Scripting

Bash passes flags to shell wrong

Hi, first post, so hello to all. I have a Bash scripting problem that is driving me a bit nutty. It involves a program called 'convert' which is part of the ImageMagick collection. Normal usage from the commandline is: $ convert -resize 120x120 inputfile.jpg outputfile.jpg This is... (7 Replies)
Discussion started by: andyj
7 Replies

10. UNIX for Dummies Questions & Answers

What is wrong with this code?

Hello everyone, can somebody tell me what is wrong with this code: while true do java myTime > myTime.log sleep 60 done I get the following error: ./myTime: Syntax error at line 1 : `while' is not matched. Thanks in advance! (6 Replies)
Discussion started by: Lem2003
6 Replies
Login or Register to Ask a Question