Jump to content

Bash Script - Printing line to file loop

spbr
Go to solution Solved by Tomsen,
2 hours ago, spbr said:

So the other commands that I want to execute after the each print will be:

  • restart network
  • ping network
  • if successful save file
  • if not, print next line and repeat same cyle

Just make a loop where you read the sourceFile line by line.

 

Something like:

while IFS='' read -r line || [[ -n "$line" ]]; do
	echo "$line" >> "$targetFile"
	# Restart network
        # Ping
        # Check ping return code
        # Evaluate based on returned code
done < "$sourceFile"

 

With a bit of research you should be able to solve it yourself in a couple of attempts.

 

Hi,

#!/bin/bash
sourceFile="file.log"
targetFile="/etc/network/interfaces"
numLines="$(wc -l < ${sourceFile})"
counter=1
if (( counter >= "$numLines" )) || [[ ! -f "${sourceFile}" ]]; then
        echo "invalid file" >82; exit 1
fi
while [ "$counter" -le "$numLines" ]; do
        echo "allow-hotplug wlan0" > "$targetFile"
        echo "iface wlan0 inet static" >> "$targetFile"
        echo "address 192.168.0.2" >> "$targetFile"
        echo "netmask 255.255.255.252" >> "$targetFile"
        echo "wireless-channel 6" >> "$targetFile"
        echo "wireless-essid grewep" >> "$targetFile"
        echo "wireless-mode ad-hoc" >> "$targetFile"
        counter=$((counter + 1))
        sed -n '"$counter"p' "$sourceFile" >> "$targetFile" && continue
done

I'm trying to print each line from sourceFile to targetFile and then wanting to run another command after each line has been printed, if unsucessful delete that line, then move onto the next. 

 

I'm seriously stuck on this one and have no idea on what to do next

 

PLEASE HELP

 

Thank you

Link to comment
Share on other sites

Link to post
Share on other sites

What other command? is it the same command each time or a different one? do you have to write to the file then run the command?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, vorticalbox said:

What other command? is it the same command each time or a different one? do you have to write to the file then run the command?

So the other commands that I want to execute after the each print will be:

  • restart network
  • ping network
  • if successful save file
  • if not, print next line and repeat same cyle
Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, spbr said:

So the other commands that I want to execute after the each print will be:

  • restart network
  • ping network
  • if successful save file
  • if not, print next line and repeat same cyle

Just make a loop where you read the sourceFile line by line.

 

Something like:

while IFS='' read -r line || [[ -n "$line" ]]; do
	echo "$line" >> "$targetFile"
	# Restart network
        # Ping
        # Check ping return code
        # Evaluate based on returned code
done < "$sourceFile"

 

With a bit of research you should be able to solve it yourself in a couple of attempts.

 

Please avoid feeding the argumentative narcissistic academic monkey.

"the last 20 percent – going from demo to production-worthy algorithm – is both hard and is time-consuming. The last 20 percent is what separates the men from the boys" - Mobileye CEO

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×