-1

for example:

file-name-has-this-length.nfo

into,

assign var1=file
assign var2=name
assign var3=has
assign var4=this
assign var5=lenght
assign ext=.nfo   # ext with or
                   #    without period 

taking any delimiters - _ . out!

My initial idea (but not limited to) is to create separated directories based on the name of the file. And any future scripts to use those names as a reference.

EDIT: I didn't find anything about "separate strings from file [names] to store their respective values". If you think this question should be moved to some other place just do it, but I need this problem solved.

This is not a duplicated!

  • 2
    try this `IFS='-_.' read -r -a parts <<< "file-name-has-this-length.nfo"; declare -p parts` – Fravadona Feb 17 '22 at 23:39
  • It outputs `declare -a parts=([0]="file" [1]="name" [2]="has" [3]="this" [4]="length" [5]="nfo")` but no variable is stored. I believe your answer can be useful in the future. But I need something I can learn from it. – Satoshi Nakamoto Feb 17 '22 at 23:47
  • 2
    @SatoshiNakamoto: Take a look at `echo "${parts[0]}"`. – Cyrus Feb 17 '22 at 23:48
  • Ok, now I have a lot of questions: what are the names of these commands, 1. are they builtin specifically for Bash or Linux API? 2. How can I change the name of the variables and 3. kind I want to understand the process behind it. 4. Is this the practical way of doing things or just a trick – Satoshi Nakamoto Feb 17 '22 at 23:51
  • 1
    @SatoshiNakamoto I'd recommend finding an introduction to shell scripting; this site is for specific, targeted questions, not general intros to broad topics like shell scripting. – Gordon Davisson Feb 18 '22 at 01:33
  • 1
    @SatoshiNakamoto : May I suggest that you google for _bash arrays_? – user1934428 Feb 18 '22 at 09:39

1 Answers1

1

Welcome to SO. Please check your attitude at the door, you will get more answers. IFS, read and <<< are all bash elements, that are explained in bash tutorials. You are expected to do your own research.

That being said...

@Fravadona proposed:

IFS='-_.' read -r -a parts <<< "file-name-has-this-length.nfo"

This splits the filename in an array, where each element is one string from the filename. The split is done on the characters defined in the IFS variable. You can read on IFS here https://www.baeldung.com/linux/ifs-shell-variable (among others that you can search for). Using the IFS to split strings is a common method in bash.

So:

parts[0] ==> file
parts[1] ==> name
parts[2] ==> has
parts[3] ==> this
parts[4] ==> length
parts[5] ==> nfo

This solution does not make any assumptions as to the number of words in the filename. An array can contain any number of elements, so this is the best - general - solution. And you can assume that the last item in the array (highest index) is the extension.

With an array, you can loop through the items to create directories. See https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays for many hints on how to use arrays.

If you can assume that the filename will always have the same number of elements, you can do:

#!/bin/bash

IFS='-_.' read -r -a parts <<< "file-name-has-this-length.nfo"

var1="${parts[0]}"
var2="${parts[1]}"
var3="${parts[2]}"
var4="${parts[3]}"  
var5="${parts[4]}"
ext="${parts[5]}"


echo "$var1"
echo "$var2"
echo "$var3"
echo "$var4"
echo "$var5"
echo "$ext"

About your questions:

1. are they builtin specifically for Bash or Linux API?

This is all pure bash, no trickery here.

2. How can I change the name of the variables

Look at the code above, var1, var2, ... can be changed to anything you want.

3. kind I want to understand the process behind it.

This is not a question, read on array usage and ask specific questions.

4. Is this the practical way of doing things or just a trick

This is the practical way of doing it. Bash is flexible, if it does what you require, it is ok. Once you have read through a bash tutorial, you can read more advanced concepts in this FAQ: http://mywiki.wooledge.org/BashFAQ

Nic3500
  • 6,706
  • 10
  • 30
  • 37