简体   繁体   中英

Bash IP IF THEN ELSE Statement

I need to run one script from several machines but each machine requires unique commands to be executed.

I thought that a bash script would be able to do this but I'm not too savvy in this department.

The code I require would look similar to the below code (or would it). I know that it's far from perfect or functional but it's really there just to help to express what I am looking for.

Could somebody please help me with this script?

#/bin/bash
IP= ifconfig eth0 | grep inet | grep -v inet6 | cut -c 7-17 

If [$IP -eq 192.168.32.1]
then mkdir IPFolder-1
more code...
else
if [$IP -eq 192.168.32.2]
then mkdir IPFolder-2
more code...
else
if [$IP -eq 192.168.32.5]
then mkdir IPFolder-5
more code...

use double brackets, spaces are important:

IP=$(ifconfig eth0 | grep inet | grep -v inet6 | cut -c 21-34)
if [[ $IP = 192.168.32.1 ]]; then
  mkdir IPFolder-1
elif [[ $IP = 192.168.32.2 ]]; then
  mkdir IPFolder-2
fi

Note: on Ubuntu 11.10, I had to use cut -c 21-34, adapt as necessary.

Be careful, ifconfig is localized, maybe it's why Sam has to cut 21-34 instead of 7-17.

Use LANG= ifconfig to have it in english everywhere.

It seems that ifconfig have other quirks to know.

(It should be more a comment than an answer, but i can't comment yet.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM