0

I currently am trying to write a script that will check if a client is hardwired into an ethernet connection before running a script to rebind to AD. My idea so far has been to list the connected network IPs and go through that to make sure they're connected. Is there a way to search through the text for a set sequence/variable type of thing? I want to check for 10.101. in the beginning of one of the IPs to see if it has a wifi connection.

This is what I have written so far `

n=0
myArray=$(ifconfig -l | xargs -n1 ipconfig getifaddr)

 

for i in ${myArray[@]}
do
    testString="$i"
    echo $i
    ((n=n+1))
done

 echo ${myArray[0]}

` This echoes all the IPs and counts how many there are.

  • forgot to add, my idea was to count how many IPs, if there are 2 then search for the wifi subnet is 10.101 which would mean the other one connected is one of our many hardwired subnets. – Jerezilla Dec 20 '22 at 18:28
  • _Possible_, yes. _Efficient_, no -- if you want it to be efficient (meaning faster than linear search), build an associative array with your values as keys. – Charles Duffy Dec 20 '22 at 18:28
  • Note that `${array[@]}` has all the same bugs as `${array[*]}`, btw; if you want the benefits of `[@]`, you need to use quotes: `"${array[@]}"`. Similarly, always `echo "$i"`, not `echo $i` – Charles Duffy Dec 20 '22 at 18:29
  • ...see [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566) – Charles Duffy Dec 20 '22 at 18:29
  • 2
    In the case of `myArray=$(...)`, though, your `myArray` variable _isn't actually an array at all_; when you parse it as an array, it has exactly one entry (the bugs I mentioned you get from leaving out quotes make it _look_ like multiple entries, but that's only an incorrect appearance you get because of bugs; the actual value is just one string, not an array of separate items) – Charles Duffy Dec 20 '22 at 18:30
  • If you wanted it to be an array, you might instead write, say, `readarray -t myArray < <(ifconfig -l | xargs -n1 ipconfig getifaddr)` – Charles Duffy Dec 20 '22 at 18:31
  • 1
    Beyond that, the question isn't specific enough to really be answerable. If you want to know whether a string has a given prefix, we have existing Q&A telling you how to do so; how is this question different from that? – Charles Duffy Dec 20 '22 at 18:32
  • (see https://stackoverflow.com/questions/2172352/in-bash-how-can-i-check-if-a-string-begins-with-some-value for an example of a preexisting Q&A entry asking how to check a string for a prefix) – Charles Duffy Dec 20 '22 at 18:33
  • BTW, re: "shell/bash" -- POSIX sh doesn't support arrays at all, so it probably makes sense to focus on a specific extended shell that does have the extensions you're looking for (so you might take the "shell/" out). The answers are different: For wider compatibility you'd use `case $value in "$prefix"*) echo "value $value starts with prefix $prefix";; esac` to check for a prefix match, with bash you can use `if [[ $value = "$prefix"* ]]; then echo "value $value starts with prefix $prefix"; fi` without needing `case`. – Charles Duffy Dec 20 '22 at 18:35
  • (`((n=n+1))` is another bashism; for compatibility with POSIX you'd need to make it `n=$((n+1))`) – Charles Duffy Dec 20 '22 at 18:37
  • Thanks everyone! I'm fairly new this is my first real script I am trying to write so sorry if this isn't a great question! – Jerezilla Dec 20 '22 at 18:55

0 Answers0