LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2019, 12:19 PM   #1
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Rep: Reputation: Disabled
Question 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

Last edited by bmxakias; 08-31-2019 at 12:21 PM.
 
Old 08-31-2019, 12:39 PM   #2
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 2,263

Rep: Reputation: 499Reputation: 499Reputation: 499Reputation: 499Reputation: 499
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""
 
1 members found this post helpful.
Old 08-31-2019, 12:45 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,078

Rep: Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903
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

Last edited by BW-userx; 08-31-2019 at 01:22 PM.
 
Old 08-31-2019, 01:12 PM   #4
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled
Ok i got it working !!!!

Thanks all of you !

Last edited by bmxakias; 08-31-2019 at 01:32 PM.
 
Old 08-31-2019, 05:07 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,993

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
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]+ )

Last edited by Firerat; 08-31-2019 at 08:11 PM. Reason: typos
 
1 members found this post helpful.
Old 08-31-2019, 08:19 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,993

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 07:59 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
undefined variable increments in PERL gaynut Programming 3 09-18-2008 09:46 AM
Are increments and decrements (++ and --) atomic on SMP and multi-core? jspenguin Programming 11 09-18-2006 12:22 AM
Memory Increments vcheah Linux - Hardware 3 08-24-2002 10:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration