opening a file given as standard input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting opening a file given as standard input
# 1  
Old 06-04-2009
opening a file given as standard input

Hi

I am trying to write a shell script which should take the file as standard input. As file(content and name both) will change for each run. It should read the file line by line. with each line I have to perform certain operation.
For example
I have i file foo, it looks like
/usr/doc/abc
/usr/xyz/xyz

I have to print name of file in each path expected output is
abc
xyz

Pl suggest me with full code
Regards
Shashi
# 2  
Old 06-04-2009
Since it's your first post here, you're asking for "full code" without showing what you tried so far, for a problem that's very basic and has been answered in a lot of threads, I'm going out on a limb here and ask: Homework?
# 3  
Old 06-04-2009
==========================================
> cat > a.sh
#!/bin/bash

exec 6<&0
exec < a.sh
while read line
do
echo $line
done
exec 0<&6
'ctrl-D'
>
>
> ./a.sh
#!/bin/bash

exec 6<&0
exec < a.sh
while read line
do
echo $line
done
exec 0<&6
==========================================

You could actually search with "redirect stdin" on google.
# 4  
Old 06-05-2009
Thanks for your reply

I had following code

#!/bin/bash

processLine(){
line="$@" # get all args
echo $line
}
FILE =""
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2
fi

exec 0<$FILE
while read line
do
# use $line variable to process line in processLine() function
processLine $line
done
exit 0


Through this code I am able to read line by line but now my requirement is
it should print some lines(say 10) then it should wait for hitting return key then print another 10 line. Just like the way we see help in linux some lines printed then it waits for hitting some key

pl suggest

Regards
Shashi
# 5  
Old 06-05-2009
a little hint:

set a counter while printing, when it reaches 10, read a carriage return from stdin (in my code, it would be '&6' and then proceed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

2. Shell Programming and Scripting

Use the content of a file as standard input

I want to use a content of a file as standard input to a program and dump the output to a file. However, when I try the following code: ./program < input.in > output.out The output.out is empty. So, how can I handle this problem? Thanks in advance! (11 Replies)
Discussion started by: Ray Sun
11 Replies

3. Shell Programming and Scripting

standard input and cron

I have a program that requires the user to enter input values while it is being run for example in bash ... ... .. echo "Enter your input" read input echo $input ... ... ...I need to schedule this program with crontab, hence a problem, cronjobs run in the background, any ideas on how to... (10 Replies)
Discussion started by: walforum
10 Replies

4. UNIX for Dummies Questions & Answers

Copying and Renaming file through standard input

Hi Geeks, I am relatively new to Unix. Trying out to achive a shell script by hard learning. Here is my requirment. 1. I have to search for specified strings that are given in .csv file in the directory to find the files for matching strings in the .csv file. 2. If match is found, copy... (1 Reply)
Discussion started by: uunniixxuusseer
1 Replies

5. Shell Programming and Scripting

Reading from standard input

So, I am new to shell scripting and have a few problems. I know how to read from standard input but I do not know how to really compare it to say, a character. I am trying to compare it to a character and anything exceeding just a character, the user will get an output message, but the program... (7 Replies)
Discussion started by: Bungkai
7 Replies

6. Shell Programming and Scripting

Reading Standard Input

Hello, I am new to scripting. How do I read multiple lines from the command line? I know read reads one line, but if I have to read multiple lines, how should I do? Thanks, Prasanna (4 Replies)
Discussion started by: prasanna1157
4 Replies

7. Solaris

standard input

Please give me any example for standard input in Solaris. (6 Replies)
Discussion started by: karman0931
6 Replies

8. Shell Programming and Scripting

change standard input ?

Dear... I have a scrpit that contains multiple read command.... when I run the script I have to enter 3 variables so that I can get the output.. but, I dont want to put those 3 inputs manually every time... I want to make a shell that reads the 3 inputs from a file. the script name is... (4 Replies)
Discussion started by: yahyaaa
4 Replies

9. Shell Programming and Scripting

How to copy from standard input

I tried copy the output files from find command into a directory. Example, find / -name core 2>/dev/null | xargs cp???? I have known that we can use xargs to execute command lines from standard input but how to use it in this case. Or I can do something besides xargs. (2 Replies)
Discussion started by: lalelle
2 Replies

10. Shell Programming and Scripting

standard input

how can i redirect standard input? i dont remember :/, though could you redirec not from a command? i mean, to redirect always stdin and stout (1 Reply)
Discussion started by: Jariya
1 Replies
Login or Register to Ask a Question