Visit Our UNIX and Linux User Community


Don't wait for "read line"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Don't wait for "read line"
# 1  
Old 08-25-2009
Don't wait for "read line"

Hi

I am writing a bash script on Solaris, that should take n arguments, either appended to the script or taken as output from the last command (similar to grep). What I don't want is that the script waits for user input. In other words:

Possibility 1:
Code:
script.sh arg1 arg2 arg3 ...

Possibility 2:
Code:
ls | script.sh

If the script is executed without any arguments, a usage message is meant to appear. I know how to handle either one of the two possibilities:

Possibility 1:
Code:
while (($#)); do
    do_something $1
    shift
done

Possibility 2:
Code:
while read line; do
    do_something $line
done

But how would this be done in combination, so that the script does not wait for user input? I hope I made myself clear.

Thanks, Stefan
# 2  
Old 08-25-2009
Hi.

Try this:

Code:
echo ARGS: $@
more 2>/dev/null | while read line; do
  echo $line
done

> ./Test
>

> echo a b c | ./Test
a b c

echo "a b c" | ./Test 1 2 3
ARGS: 1 2 3
a b c

I don't know if it's the best solution, but it seems to do the trick.

Last edited by Scott; 08-25-2009 at 10:21 AM..
# 3  
Old 08-25-2009

Code:
args=$#

while [ $# -gt 0 ]
do
  do_something "$1"
  shift
done

if [ $args -eq 0 ]
then
  while read line; do
    do_something "$line"
  done
fi

# 4  
Old 08-25-2009
That was the other option, of course - unless there is nothing coming from stdout.

Last edited by Scott; 08-25-2009 at 03:53 PM..
# 5  
Old 08-25-2009
Hi all

Many thanks for your answers. As far as I understand the solution of cfajohnson, the script waits for input in the second while loop, if it is being called without any arguments (maybe I did something wrong?). However, the solution of scottn is doing exactly what I want. With that I can get a list of arguments either via appending them to the script or from stdout of the last command.

Thanks again.
Stefan

Previous Thread | Next Thread
Test Your Knowledge in Computers #361
Difficulty: Medium
The rmdir command remove directories, even if they are not empty.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

calling a shell script in background and wait using "wait" in while loop

Hi, I am facing a strange issue, when i call a script from my while loop in background it doesnt go in background, despite the wait i put below the whil loop it goes forward even before the process put in background is completed. cat abc.txt | while read -u4 line do #if line contains #... (2 Replies)
Discussion started by: mihirvora16
2 Replies

4. Programming

fgets read file line with "\n" inside

Hi, I have a string like this, char str ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line"; if I want to use strtok() to seperate the string, which token should I use? I tried "\n", "\\n", either not working. peter (1 Reply)
Discussion started by: laopi
1 Replies

5. Shell Programming and Scripting

speeding up bash script with "while read line"

Hello everybody, I'm still slowly treading my way into bash scripting (without any prior programming experience) and hence my code is mostly what some might call "creative" if they meant well :D I have created a script that serves its purpose but it does so very slowly, since it needs to work... (4 Replies)
Discussion started by: origamisven
4 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

defining a variable using multiple "entries/columns" within a line using read

Hello All, I am trying to figure out how to use the read command. I have a txt file that has rows looking something like this: 1 2 3 4 5 6 7 8 9 where each number represents an entry of various characters deliminated by tabs. My goal is to set entries 2-7 as a variable/string that I... (3 Replies)
Discussion started by: Torinator
3 Replies

8. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

9. Shell Programming and Scripting

script to read a line with spaces bet " " and write to a file

Hi, I need a command in UNIX KSH below is the description... MAPPING DESCRIPTION ="Test Mapping for the calid inputs" ISVALID ="YES" NAME ="m_test_xml" OBJECTVERSION ="1" VERSIONNUMBER ="1" unix ksh command to read the DESCRIPTION and write to a file Test Mapping for the calid inputs... (3 Replies)
Discussion started by: perlamohan
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

Featured Tech Videos