1820013 Members
3599 Online
109608 Solutions
New Discussion юеВ

Re: Shell programing

 
SOLVED
Go to solution
Igor Sovin
Super Advisor

Shell programing

I have text file with several strings in it.
for example
more 123.txt
aa
bb
cc
dd

I need to make script that could read these strings from 123.txt and write them in another txt file.
18 REPLIES 18
RAC_1
Honored Contributor

Re: Shell programing

cp 123.txt what_ever.txt
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor
Solution

Re: Shell programing

while read line; do

echo ${line}

done < inputfile >

You can copy all the contents if you are not going to any changes as,

cp

--
Muthu
Easy to suggest when don't know about the problem!
Peter Godron
Honored Contributor

Re: Shell programing

Igor,
if you insists on a script:
#!/usr/bin/sh
while read record
do
echo $record >> output.txt
done < 123.txt
Muthukumar_5
Honored Contributor

Re: Shell programing

Are you going to do any shell program or changes in content? or simply that contents itself?!

More ways to simply copy the file contents to new.

cat file > newfile

--
Muthu
Easy to suggest when don't know about the problem!
Igor Sovin
Super Advisor

Re: Shell programing

I have the following script:

for i in 1 2 3
do
command ${variable}
done

I need to get aa then bb and cc from 123.txt and put it into my script instead of variable.
How to do that?
Muthukumar_5
Honored Contributor

Re: Shell programing

Use like:

for i in 1 2 3
do
while read variable;
do
command ${variable}
done < 123.txt
done

That is it.

--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Shell programing

Can you just ellaborate your requirement to give effect script solution!? In the for loop you are not using i variable anywhere.

Post requirement as:

1) input
2) expected output from input

--
Muthu
Easy to suggest when don't know about the problem!
Igor Sovin
Super Advisor

Re: Shell programing

the problem is :

1. I have txt file containing list of remote hosts.
host1
host2
host3 etc
2. I need to do script that reads host1 from txt file and ssh to host1 do the command then exit from host1, and this action must be done for all hosts ( host2, host3.. etc).
Muthukumar_5
Honored Contributor

Re: Shell programing

1. I have txt file containing list of remote hosts.
host1
host2
host3 etc
2. I need to do script that reads host1 from txt file and ssh to host1 do the command then exit from host1, and this action must be done for all hosts ( host2, host3.. etc).

>>>>>>>>

Simply as,

put all hosts in > hosts file. So host1,host2 and etc are in hosts file.

while read host;
do
ssh @${host} 'command'
done < hosts


If you don't want to specify the hostname then,

while read host;
do
ssh ${host} 'command'
done < hosts

will do that.


PS: Hope you have set key transaction between hosts for not asking password. Else it will ask password.

--
Muthu
Easy to suggest when don't know about the problem!
Igor Sovin
Super Advisor

Re: Shell programing

let me try..
Igor Sovin
Super Advisor

Re: Shell programing

I've created two files

root@lev /Ignite_Bckp>more test.sh
while read host;
do
ssh ${host}
done < hosts_test.txt

and file containing hosts (in the same dir)

root@lev /Ignite_Bckp>more hosts_test.txt
adam
kate

when executing script test.sh I get error:

root@lev /Ignite_Bckp>chmod +x hosts_test.txt
root@lev /Ignite_Bckp>./hosts_test.txt
./hosts_test.txt: adam: not found.
./hosts_test.txt[2]: kate: not found.

Hein van den Heuvel
Honored Contributor

Re: Shell programing


You reversed the files and are trying to exectue the data (.txt) file.

Try:

chmod +x test.sh
./test.sh


Also... I generally preferr to keep the parameter out of the script file. You your case that means removing the "< node_test.txt" fromt eh .sh

The activate the shell script providing whatever data is appropirate at the moment, possibly even entering lines manually:

./test.sh < node_test.txt

Hein.
Peter Nikitka
Honored Contributor

Re: Shell programing

Hi,

a simple 'ssh' will leave stdin open, so a successful connection will read all but the first value of the loop.
Use:

while read host
do ssh -n $host
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Patrice Le Guyader
Respected Contributor

Re: Shell programing

Demat,

Try to enable the public key mechanism.

Look at this procedure and check for the following lines in sshd.conf file :
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile ~/.ssh/authorized_keys
These lines should be uncommented.

After this you'll be able to do : sftp/scp/ssh between this hosts without password asking.

Another possibility for shell scripting ;
#!/sbin/sh
for SERVER in `cat hosts.txt`
do
ssh $SERVER < command 1
command 2
command n
EOF
done

Hope this help
Kenavo
Pat
Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
Jannik
Honored Contributor

Re: Shell programing

#!/usr/bin/ksh
# use file /tmp/host.list
# file name is : xxxxx

for i in $(cat /tmp/host.list)
do
echo "do something on : $i"
ssh root@$i "command;command"
done
jaton
Igor Sovin
Super Advisor

Re: Shell programing

I'm a novice in shell programming.
Thanks to all for advices. My scrips works fine now.
Arunvijai_4
Honored Contributor

Re: Shell programing

Hi Igor,

You can close this thread now.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Igor Sovin
Super Advisor

Re: Shell programing

have found a solution to this question as seen in the comments below.