2

I have ksh script, which is running the below command:

mkdir -p down/$JENKINS_URL

the thing is that some $JENKINS_URL (string) includes HTTP:// and in some HTTPS://, which is causing the creation of two subdirectories under down folder, and then subdirectories for each of them. how can I avoid it and create directly the folders name without HTTP or HTTPS under down folder?

  • 2
    mkdir -p "down/${JENKINS_URL#*://}" -- don't omit the quotes and be sure you're in control of that variable: it may break your system via ../../../../some/path. – mosvy Jan 19 at 10:19
4

Parameter substitution can be used to strip string prefixes from the JENKINS_URL. From the ksh man page:

${name#pattern}, ${name##pattern}

If pattern matches the beginning of the value of parameter name, the matched text is deleted from the result of substitution. A single # results in the shortest match, two #'s results in the longest match.

As an example:

JENKINS_URL="http://abc"
mkdir -p "down/${JENKINS_URL#*://}"
JENKINS_URL="https://def"
echo mkdir -p "down/${JENKINS_URL#*://}"

The above script will strip any protocol prefix. The result of running the script is:

mkdir -p down/abc
mkdir -p down/def
  • 1
    Why ##? It seems to me shortest match # would be more correct... – filbranden Jan 19 at 11:18
  • 1
    @filbranden No idea. my brain said # but my fingers somehow typed something else. Thanks for pointing that out. – kaylum Jan 19 at 11:26
  • Tnx! it worked like magic – arielma Jan 19 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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