Jump to content

Linux script that writes and displays an ip address.

I was doing a script in Linux that saves and displays the ip address in cmd

 

nano address

 

Then at the very beginning of the file I wrote #!/bin/bash

So that Linux knows this is a script.

 

Further in the file I wrote: 

ifconfig > plik.txt
grep "inet" > plik.txt
cat plik.txt

 

Then I gave the script permissions:
chmod + x adres

 

I ran the script:
./address

 

 

I'm still sending photos. I did the script creation right this is how each script is created.
I also ran the script well and so each script runs.

Maybe some command is wrongly written in the script.

 

1.jpg

Link to comment
Share on other sites

Link to post
Share on other sites

It doesn't work. I'll leave it for you to figure out why. So you can learn 😉

 

Spoiler

hints: spelling & permissions

 

"You don't need eyes to see, you need vision"

 

(Faithless, 'Reverence' from the 1996 Reverence album)

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, Dutch_Master said:

It doesn't work. I'll leave it for you to figure out why. So you can learn 😉

 

  Reveal hidden contents

hints: spelling & permissions

 

I know it doesn't work. I was looking on the internet why it doesn't work but I don't know. When I enter the same letters in the terminal, it works normally, but I don't know why the same commands in the script don't work.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, GamerGry123 said:

ifconfig > plik.txt
grep "inet" > plik.txt
cat plik.txt

  1. Can you explain what you think each line does? And in particular what you think ">" does?
  2. Is that really the best way of solving this? Aren't you interesting in taking the output of ifconfig and directly filtering for what you are looking for?
Link to comment
Share on other sites

Link to post
Share on other sites

#!/bin/bash

#Set the Device to be used.
DEVICE="enp4s0"

#Set the File you want to save to.
OUTPUT="./local_ip.txt"

#Obtain IP Address
IP=`ifconfig $DEVICE | egrep -o 'inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | cut -d' ' -f2`

#Ouput to File
echo $IP > $OUTPUT
Link to comment
Share on other sites

Link to post
Share on other sites

I would do like this:

 

#!/bin/bash

outputfile=/tmp/myip

ip -4 a l  $(ip route |head -1 | awk '/default/ { print $5 }') |  awk '/inet/ { print $2 }' | awk -F'/' {'print $1'} | tee ${outputfile}

 

 

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

×