JosephFY
JosephFY

Reputation: 17

Run commands in text file in bash

How can I run a command from a line inside a text file, one line of my text file looks like this

echo "RouterPing;`ping -c4 -w4 -q DeviceIP| tail -2 |awk '{print}' ORS=' '`;$(date)" >> somefile.txt &

I have a file that has thousands of lines that being generated by external program and want to execute every line in it. I need each line to be run exactly as if I am running it from bash shell

Upvotes: 0

Views: 406

Answers (4)

Tamar
Tamar

Reputation: 679

with dot ('.') (I removed the & because if the command runs in background it wont work)

echo "RouterPing;`ping -c4 -w4 -q DeviceIP| tail -2 |awk '{print}' ORS=' '`;$(date)" >> somefile.txt 

. somefile.txt

Upvotes: 0

jgmh
jgmh

Reputation: 689

you$ bash somefile.txt

Just make sure your file is executable (chmod 744 somefile.txt)

Upvotes: 1

pritesh agrawal
pritesh agrawal

Reputation: 1225

you can use below , but i would highly not recommend executing 1000 commands from a file ,

#!/usr/bin/bash
filename="$1"
while read -r line
do
$line
   done < "$filename"

How to use ./this_file_name.sh file_with_commands

Upvotes: 1

zhenguoli
zhenguoli

Reputation: 2308

You can just run:

bash file.txt 

Upvotes: 1

Related Questions