0

As you know, you cannot use the mkdir command with slashes.

I am creating an automation and what i did first, was to nmap every IP Addresses i chose, "192.168.1.0" for example, as the first argument.

then, make a folder named the first argument, which is "192.168.1.0" = $1 ==> ./script.sh 192.168.1.0

but, I want the user to put the IP Address like this: "192.168.1.0/24". How do i use the mkdir command now to still create a folder named "192.168.1.0" without /24 as the first argument still?

I tried many variations with sed commands. I hope i was clear enough to explain myself.

3
  • Does Kali use bash, or some more basic shell? If it uses bash, try mkdir "${1%/*}". The %/* part means "remove something matching /* from the end of the argument". Commented Aug 14, 2022 at 1:36
  • The linked duplicate's answers suggest quite a lot of potential solutions; that said, the one I would use is mkdir "${1%%/*}" Commented Aug 14, 2022 at 1:44
  • BTW, there's no need to duplicate information from your question's tagging in its title. (Also, generally speaking, tagging should be limited to things that can help put it in front of the right experts; the fact that the data came from nmap has nothing to do with how you process it for mkdir, f/e, and someone who knows nmap well will have no better ability to answer this question than someone who's never heard of it) Commented Aug 14, 2022 at 1:46

1 Answer 1

0

Here's a possible solution using cut:

#!/bin/bash
dir_name=$(echo "$1" | cut -d '/' -f 1)

mkdir -v "$dir_name"

Output: example

This obviously doesn't validate the IP address, for that you could do some parsing with a regex.

4
  • I need the folder name stays as the IP Address itself without /24 or _24. This is a great example of how i need it to be looks like: i.imgur.com/5CUHS6b.png
    – Oded
    Commented Aug 14, 2022 at 1:48
  • Not sure if I have understood at this point, so you want to create a folder named from example 192.168.1.0 when you pass 192.168.1.0/24? You want to remove the "/24"?
    – mikyll98
    Commented Aug 14, 2022 at 1:59
  • Exactly what i want to do.
    – Oded
    Commented Aug 14, 2022 at 11:46
  • @Oded I've edited the comment with a possible solution
    – mikyll98
    Commented Aug 14, 2022 at 13:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.