Friday, September 2, 2022

[SOLVED] How do i remove slashes in mkdir command?

Issue

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.


Solution

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.



Answered By - mikyll98
Answer Checked By - Senaida (WPSolving Volunteer)