Not able to put ls -lR command in background


 
Thread Tools Search this Thread
Operating Systems Linux Not able to put ls -lR command in background
# 1  
Old 02-11-2014
Not able to put ls -lR command in background

Hi All,

i am trying to put ls -lRt command to run in the background but it fails.
i continue to get output on screen and didnt get the command prompt right after i put the command in background.
command i am using is
Code:
 
ls -lRt &

i am using bash.

Can someone let me know how to resolve this.
# 2  
Old 02-11-2014
And where do you want to fetch the data?

You might like/use:
Code:
ls -lRt > ~/fetched-ls.txt &

That will redirect all output (of stdout) to the file $HOME/fetched-ls.txt, and doing so in background.
Just be sure the process is done before using the outputfile.

Hope this helps
# 3  
Old 02-11-2014
thanks 'sea'...is it possible to put the output of command (stdout) in the background without redirecting.
in a nutshell after i fire command ls -lRt & , command prompt should return so that i can execute another command. and once the execution of the ls -lRt is over in the background it should display the stdout on the screen.
# 4  
Old 02-11-2014
AFAIK:
You can either redirect the output somewhere else or not.
But you cannot 'buffer' it, unless you use a file, variable or reduce the output by using pipes.

In case of a variable you could try: DATA=$(ls -1Rt) & then continue your work, and when its done, type: echo "$DATA"

Otherwise, please explain what you are trying to achieve.

hth
# 5  
Old 02-12-2014
thanks sea...i am new to this unix world..so trying to learn the actual difference between the foreground and background process.

Now i have clear idea about it..thanks for the information and knowledgeSmilie
# 6  
Old 02-12-2014
That will not work, since the variable will get set in a subshell and therefore will not retain its value once it returns..

You could use a named pipe:
Code:
mkfifo somepipe
ls -lR > somepipe &
echo hello
cat < somepipe
rm somepipe

Or in bash / ksh93
Code:
exec 3< <(ls -lR)
echo hello
cat <&3

In ksh93 you can use a co-process
Code:
ls -lR |&
echo hello
while IFS= read -rp line
do
  printf "%s\n" "$line"
done

In bash 4 also:
Code:
coproc ls -lR
echo hello
cat <&${COPROC[0]}


Last edited by Scrutinizer; 02-12-2014 at 03:58 AM..
# 7  
Old 02-12-2014
thanks Scrutinizer Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Put currently running script into background

Hi All, Suppose I have a script and inside it I want/need to put it into background. I need the script to not react to SIGHUP signals. I tried: #!/bin/bash echo "" > test_disown mypid=$$ echo "PID=$mypid" ( kill -SIGSTOP $mypid jobs > myjobs #disown -h <job-spec> #kill -SIGCONT $mypid )... (6 Replies)
Discussion started by: JackK
6 Replies

2. UNIX for Dummies Questions & Answers

several command in background

hi everyone! I would like to know if someone can tell me how to run several command in background on one line. I already tried thisi: cmd1 &; cmd2 &; cmd3 & --->produce an error syntax cmd1 ; cmd2 ; cmd3 & --->don't execute in bg cmd1 & cmd2 & cmd3 & --->execute... (1 Reply)
Discussion started by: Eti38
1 Replies

3. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

4. Shell Programming and Scripting

How can put a background process to the foreground

Hi, guys: I am working on my own shell using c. When I put a process into the background, how can I put it back to the foreground using tcsetpgrp? Thanks (3 Replies)
Discussion started by: tomlee
3 Replies

5. UNIX for Dummies Questions & Answers

kill a command I am running / then put it to background

I have a command running in the foreground (and so my term window is locked up) and I want to kill it, then resume it in the background and go home. It is creating a zip file, and the file will be written to the current directory - no std in / std out issues. How do I do this? Kill it with a... (3 Replies)
Discussion started by: hindesite
3 Replies

6. Shell Programming and Scripting

How to put scp in background inside expect

Gents, I have a wrapper script passing couple of information to an expect script mainly responsible for spawning scp and providing the password (which is transmitted down from the main script). the main script prepare the transfer to couple of servers, idea being to transfer the files in... (3 Replies)
Discussion started by: luc3004
3 Replies

7. Shell Programming and Scripting

put an interactive script in background after taking input

i am trying to find a way to put an interactive script in the background after taking input from the user a few test lines i was trying: date echo "Enter location" LOCATION= read LOCATION sleep 100 sleep 200 date for this small example i want the script to as the user for the... (7 Replies)
Discussion started by: epsilonaurigae
7 Replies

8. HP-UX

where to put autorstart command?

hi everyone, I want to run some command automatically when the OS boot. In Linux, I found that we just put it into /etc/rc.local file; but I don't know where to put it in HP-UX system. please, help me........ thanks very much, :) (2 Replies)
Discussion started by: tataxin
2 Replies

9. UNIX for Advanced & Expert Users

How to put vi file_names in background mode?

HI, When I used to work with Sun Solaris, I used to open multiple files and without closing it I will put them in background mode(using Ctrl-ZZ). In this way I will have 4-5 files open and using fg %2, I can vi the second file..while the rest of the files will be in background mode! I want to... (4 Replies)
Discussion started by: thiagoo
4 Replies

10. UNIX for Dummies Questions & Answers

How to store output in variable when put in background

Hi, How do I store following command output: export RESULT=`date` & It works when I do : export RESULT=`date` But what I need is when command put it background, I also need that output going to RESULT variable. Is there any way ? Thanks Sanjay (1 Reply)
Discussion started by: sanjay92
1 Replies
Login or Register to Ask a Question