LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-20-2009, 01:58 AM   #1
moo-cow
Member
 
Registered: Mar 2006
Distribution: Debian
Posts: 105

Rep: Reputation: 25
Bash: Handling input from file and keyboard


Hi!

I have this little shell script which copies file names taken from inputfile:

Code:
while read line; do
   cp -i $line something
done < inputfile
The problem is that the "-i" option of cp has no effect since stdin comes from inputfile and not from the user. Is there any way to circumvent this? I would somehow need to tell the shell to take input from the keyboard just for the cp command (?).

Thx,
moo-cow
 
Old 01-20-2009, 02:12 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Code:
for file in `cat t3.t`
do
    cp -i $file t2.t
done
works for me.
 
Old 01-20-2009, 02:27 AM   #3
laptop-power-battery
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
good
 
Old 01-20-2009, 03:22 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The easy way to accomplish your task is
Code:
#!/bin/bash
cp -i $(cat inputfile) dirname
using command substitution as in the example above will result in a list of files separated by spaces, since in this way the shell strips out the newline characters. The resulting command syntax is valid, provided the last argument is the name of an existing directory.

The hard way, following your example is
Code:
exec 8< inputfile
while read -u8 line
do
  cp -i $line copy
done
exec 8<&-
This will associate the input file to file descriptor 8 and the read statement will accept input from this file descriptor through option -u. In this way the standard input remains associated to the terminal. Also notice that the -i option of the cp command ask confirmation before overwriting a file only, not for each file to copy.

For other uses take in mind you can restore standard input from the terminal using exec < /dev/tty but in your case the while loop would have stopped its execution, having lost connection to the input file.
 
Old 01-24-2009, 09:21 AM   #5
moo-cow
Member
 
Registered: Mar 2006
Distribution: Debian
Posts: 105

Original Poster
Rep: Reputation: 25
Thanks for the solutions!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SUSE10.1 : bash query : saving input to file linuxFNG Linux - Newbie 3 03-07-2008 01:23 PM
how to simulte keyboard input from a bash script jmling Linux - General 5 10-26-2007 05:33 PM
how to simulte keyboard input from a bash script jmling Linux - General 3 10-24-2007 08:17 PM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 04:00 PM
logging all keyboard input/output to a file div Linux - General 0 02-20-2001 06:29 AM


All times are GMT -5. The time now is 10:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration