LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to create a variable from a command that increments the folder name? (https://www.linuxquestions.org/questions/programming-9/how-to-create-a-variable-from-a-command-that-increments-the-folder-name-4175660121/)

bmxakias 08-31-2019 12:19 PM

How to create a variable from a command that increments the folder name?
 
Hello

I am using this code and is working as expected....

Code:

# Increment $N as long as a directory with that name exists
while [[ -d "/backup/$DOMAIN/$DATE/database/inc$N" ]] ; do
    N=$(($N+1))
done

if ls -A $TARGETDIR/* > /dev/null 2>&1; then
    mkdir -p "/backup/$DOMAIN/$DATE/database/inc$N"
fi

When the $TARGETDIR is not empty it creates a folder and increase it by 1 every time the script runs like:

Quote:

/backup/$DOMAIN/$DATE/database/inc1
/backup/$DOMAIN/$DATE/database/inc2
/backup/$DOMAIN/$DATE/database/inc3
On the above code i want to run a command that needs the destination path like:

Code:

backup bla bla bla --target-dir /backup/to/dir

How should i know the created folder every time the script runs if it is inc1 or inc4 and create a variable from this command when it runs?

Code:

mkdir -p "/backup/$DOMAIN/$DATE/database/inc$N"
Thank you

teckk 08-31-2019 12:39 PM

Save the directory name as a variable then make it.
Code:

mydir="/backup/$DOMAIN/$DATE/database/inc$N"
mkdir -p -- "$mydir"

Code:

mydir="/backup/$DOMAIN/$DATE/database/inc$N"
msg=$(mkdir -p -- "$mydir" 2>&1)
echo "$msg"

You need to quote your variables.

Code:

a=cats
echo "I like "$a""


BW-userx 08-31-2019 12:45 PM

I almost understand what you are trying to say, I think.
you have a script that creates dirs and adds 1 for each new sud-dir it created.
when you then backup you want to know where to start or left off so you can pick up from there?

You're going to need to look first to see where you're at then set it from where you left off at. Using your $N value.

you should already know your values within your variables.
Code:

/backup/$DOMAIN/$DATE/database/inc$N"
do a search on it, putting them in a file then sort them, get the value left off at, so now you can pick up from there?

that $DATE might mess you up, if it is using current date.
/backup/$DOMAIN/*/database/inc$N"

Code:

find /backup/$DOMAIN/*/database/inc* -type d >> ckfile
or
find find /backup/$DOMAIN/*/database -type d >> ckfile

check last entry to get number left off at, then use that to start your incurrent.
--
just straight forward create incremented dir, check them, get number I left off at.
Code:

#!/usr/bin/env bash

dododir=$HOME/test/$(date)/backup
N=0
for i in {1..15} ; do
 mkdir -pv "$dododir"/inc"$N"
        ((N++))
done
#create list of dirs already created
find "$HOME"/test/*/backup -type d >> ~/testscripts/ckleftoff

echo
#get last line in the file, to get number left off at
lastline=$(awk '/./{line=$0} END{print line}' ~/testscripts/ckleftoff)

#working example of what dir structure looks like

#/home/userx/test/Sat Aug 31 13:07:24 CDT 2019/backup/inc14

#strip everything off left to right up to last /
lastline=${lastline##*/}

#get the number off the tail piece inc$N
lastline=$(echo -e $lastline | egrep -o [0-9]+ )

#number of where to pick up from needs +1
echo;echo $lastline


bmxakias 08-31-2019 01:12 PM

Ok i got it working !!!!

Thanks all of you !

Firerat 08-31-2019 05:07 PM

Code:

#!/bin/bash
Dir="$1"
Dirs=(${dir}*) # put all the dirs into array
# it no dir exist, the one element will be the base dir
DirCount=${#Dir[@]}
#mkdir -p "${dir}$(( ${DirCount} + 1 ))"
# fix typo
mkdir -p "${Dir}$(( ${DirCount} + 1 ))"

That makes some lazy assumptions , e.g.
on filesystem you have
/path/to/some/directory1
/path/to/some/dir1

./script.sh /path/to/some/dir
will make /path/to/some/dir3

safer
Code:

#!/bin/bash
Dir="$1"
Dirs=()
for i in ${Dir}*;do
    [[ ${i} =~ ${Dir}[0-9]+ ]] && Dirs+=("${i}")
done

#mkdir -vp "${dir}$(( ${#Dirs[@]} + 1 ))"
# fix typo
mkdir -vp "${Dir}$(( ${#Dirs[@]} + 1 ))"

The danger with that one is
/path/to/some/directory1
/path/to/some/dir1
/path/to/some/dir3

will just remake /path/to/some/dir3
because 2 + 1 = 3

PS

Code:

lastline=$(echo -e $lastline | egrep -o [0-9]+ )
# no need for echo
lastline=$(<<<$lastline egrep -o [0-9]+ )


Firerat 08-31-2019 08:19 PM

fixed typo ( dir should be Dir )

also wanted to point out that the script would break if passed a dir ending /

e.g.
./script.sh /path/to/dir/

a fix would be

Code:

Dir="${1%/}"
the %/ removes the trailing /

https://www.tldp.org/LDP/abs/html/pa...stitution.html


All times are GMT -5. The time now is 09:13 PM.