Why can't embed commands like fg or bg in a shell script ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why can't embed commands like fg or bg in a shell script ?
# 1  
Old 04-12-2009
Why can't embed commands like fg or bg in a shell script ?

Hi

Can someone explain in an easy way that why can't embed commands like fg or bg in a shell script ?
# 2  
Old 04-12-2009
Hi.

Perhaps you need monitor mode on -- fg: no job control, in script - Linux Forums ... cheers, drl
# 3  
Old 04-13-2009
Thanks for the reply. With your input I further get the following information,

  1. job control is by default turned off in non-interactive mode (shell script runs in non-interactive mode -- Bash Reference Manual). So to enable job control, we need set -m explicitly.
  2. "Typically, Job Control is enabled for the interactive shell only. Non-interactive shells typically do not benefit from the added functionality of Job Control." sh(1) - standard and job control shell and command interpreter (man pages section 1: User Commands) - Sun Microsystems
  3. It seems that I am not the only one feel confused about this statement Smilie, in a discussion in stackoverflow, Why can't I use job control in a bash script? - Stack Overflow, someone else asked "So why doesn't script-based job-control work, and what makes it a bad practice (aka 'stupid')?". Although there some answers for it, I still feel this controlling-terminal-job-control-foreground-background thing not so easy to get.
  4. Initializing the Shell - The GNU C Library -- "A subshell that runs non-interactively cannot and should not support job control. ... this allows the non-interactive shell and its child processes to be treated as a single job by the parent shell." Even harder to get Smilie
So can someone further elaborate on this ?

Thanks!!
# 4  
Old 04-13-2009
The purpose of having job-control is to regain control of your command prompt
momentarily. There is no programmatic reason to do this within a script.

For example, if you do this:

Code:
sleep 16 &
fg

What was the point in putting it in the background in the first place?

Of if you do this:
Code:
sleep 16 ### or other long process
bg

The script will never hit the "bg" command until the first command finishes.
# 5  
Old 04-20-2009
PHP

I happened to see this thread while searching for a solution to an odd bash scripting problem. It seems that there is in fact a use for job control in a shell script, though it may simply be a workabout for an existing bug.

Here I have a simple two line script "test.sh", it runs mplayer in the background and waits for an "Enter". (My actual use is for a sshfs wrapper, but the example uses mplayer as it is more widely available)

Code:
#!/bin/sh
mplayer abc.ogg </dev/null &
read

If I were to run xterm, manually run the script with "./test.sh", then close xterm, everything works as expected. mplayer continues to run in the background.

But if I were to run this script via "xterm -e ./test.sh". mplayer runs as expected, but when "Enter" is pressed, the script end, xterm closes, and mplayers dies. The same happens if I were to use gnome-terminal. Doing a "ps -axf" after mplayer starts, but before hitting "Enter", I see that mplayer is still in the foreground process group.

By modifying the script to the following, things works as expected again.

Code:
#!/bin/sh
set -m
mplayer abc.ogg </dev/null &
read

I have no idea why this is so, but it would seem that others have encountered similar problems (but without solutions):

sshfs in bash script - won't run in called terminal? - LinuxQuestions.org
Command works fine in terminal, doesn't work in the run command application...!? [Archive] - Ubuntu Forums
[ubuntu] sshfs script (for automatic mount) [Archive] - Ubuntu Forums

Anyone knows if this is a bug in bash / xterm? My guess is that when running without job control, the & don't actually cause the process to be removed from the foreground process group immediately, but only after the end of the script. Somehow, this step was missed when running the script via "xterm -e". This is just my speculation, so hopefully someone more knowledgeable can shed some light on the matter.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Two exec commands in one shell script?

Hi Folks - Is there a way to add two execs to one script? For instance, I need to redirect the stdout and stderr to two separate directories. I want to do this: #::-- Direct STDOUT and STDERROR to repositories --::# exec 2>"${_ERRORFILE}" > "${_LOGFILE}" exec 2>"/new/path/file.err" >... (7 Replies)
Discussion started by: SIMMS7400
7 Replies

2. Shell Programming and Scripting

Embed image to the html script

hi, trying to embed an image to the html file to send out as an email. img src="data:image/jpeg;base64,$(base64 /home/test/abc.jpg but getting error as file not found after it aplies base64 on the file. (8 Replies)
Discussion started by: ATWC
8 Replies

3. Shell Programming and Scripting

How to embed sql query into our shell script?

Hi I would like to embed a sql query in my shell script. Also, before any the sql query is executed, i would like to validate username and password. (1 Reply)
Discussion started by: arghadeep adity
1 Replies

4. UNIX for Advanced & Expert Users

Embed tcl in ksh93 script

Hello everyone, I am trying to embed some tcl code inside a ksh93 script but I am not having any success. I even tried the simplest of code, something like this: . . jk=$(echo $(tcl << | write_file junkme "test"' | )) just to see if a file gets written. When I run there are no errors, but ... (3 Replies)
Discussion started by: gio001
3 Replies

5. Shell Programming and Scripting

How to embed commands in awk search

Hello; When I try: awk '/$(date "+%y\/%m\/%d")/,0' It errors with: awk: There is a regular expression error. Invalid pattern. Is there anyway to make this work ?? Thank you (2 Replies)
Discussion started by: delphys
2 Replies

6. Shell Programming and Scripting

Embed commands into an expr command? (so I only store result)

Hi Can I thank the advice I've received on here previously, working examples are helping me teach myself BASH fairly quickly and I've written some pretty complex scripts now (even if I do say so myself). Anyhooo..... I'm writing a script that handles lots of text files and does various... (3 Replies)
Discussion started by: Bashingaway
3 Replies

7. Shell Programming and Scripting

writing a shell script of commands

Can anyone help me out in visualizing on what is the logic behind simple unix commands. For Eg: ls command lists files and directories, how it displays I need to know the source code for commands like this. (1 Reply)
Discussion started by: rahul_11d
1 Replies

8. Shell Programming and Scripting

shell script to run a few commands help!

Hi friends this is first post i am very new to shell scripting so i require your expertise to do the following thank u I need to write a shell script which will run the following commands pg_dump bank > backup(Enter) Wait for bash prompt to appear coz it indicates that the command is... (23 Replies)
Discussion started by: perk_bud
23 Replies

9. Shell Programming and Scripting

How to embed shell script in a awk code

I have written a code to extract comma seperated values from a file and assign them to a variable inside awk code.i want to use one of these variable to be used in wget function i.e to pass the siteurl.How i can implement the shell command wget inside the awk for loop.code snippet is shown below. ... (8 Replies)
Discussion started by: rajbal
8 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question