cancel
Showing results for 
Search instead for 
Did you mean: 
jeesshnasree
Starfighter Starfighter
Starfighter
  • 633 Views

pass more arguments in read command in bash script

 Hi All,

 

Please provide me many arguments in read command .

explain clearly now . 
1) i have a file 40 names in the file .

 

scenario with explanation :

2) read -p ( one argument is aware ) but how to pass  any arguments from file .
Please guide me , how to pass argument from  a file then  pass name  like 1 or 4 or 5 or 39 name as argument  in 40 lines of the file . 

 

How to provide required arguments in a file manually at a time execute read command in bash script .

Please provide me valuable information .

 

I am waiting for your valuable guidance .

 

Regards,

 

Jeesshnasree

 

3 Replies
Tracy_Baker
Starfighter Starfighter
Starfighter
  • 613 Views

To pass multiple items using the read command, separate the variable names with spaces:

Tracy_Baker_0-1722274647373.png

when run:

Tracy_Baker_1-1722274688770.png

 

If you are reading lines from a file, that's different. You probably need a loop to do that - one that reads a file:

Tracy_Baker_2-1722274928839.png

In this case, the file names.txt has 40 lines in it - one name per line:

john
paul
george
...
ringo

The script reads (using read) one line at a time in a while loop. During each iteration, the line from the file is stored in the variable named linein. They are then displayed.

./test.sh

john
paul
george
...
ringo

 

Once read, the data from the file (the names, in this case) can be processed in some way. If all of them need to be stored as separate variables within the script, an array would be a good thing to use.

 

Program Lead at Arizona's first Red Hat Academy, est. 2005
Estrella Mountain Community College
jeesshnasree
Starfighter Starfighter
Starfighter
  • 490 Views

Hello @Tracy_Baker ,

 

 

Thank you .


Is it possible create a file with as per aguments & that arguments need to store  as line by line in the file .

if possible as mentioned criteria then please share me script .

I am waiting for your valuable information .

 

Regards ,

Jeesshnasree

shura
Flight Engineer
Flight Engineer
  • 471 Views

Hello

May be the example below solve your question.

root@notebook:/tmp# cat names.txt
one
two
three
four
five

root@notebook:/tmp# cat test.sh
#!/bin/bash
COUNT=1
while read linein
do
if [[ $COUNT -eq $1 ]] then
echo $linein
exit 0
fi
COUNT=$(( $COUNT + 1 ))
done

Example:

root@notebook:/tmp# ./test.sh 3 < names.txt
three

Join the discussion
You must log in to join this conversation.