LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-02-2019, 10:10 AM   #16
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled

Code:
while [[ -d "$INCDIR/inc$N" ]] ; do
    N=$(($N+1))
done

if ls -A $TARGETDIR/* > /dev/null 2>&1; then
    mkdir -p "$INCDIR/inc00$N"
    MYDIR="$INCDIR/inc00$N"
    touch $MYDIR/file.txt
fi
Another thought is to ask for a user input by folder name like:

Which inc folder you want to use bla bla bla but again if the user type inc001 or inc004 i must find a way to identify that and run for inc001 one command and for inc004 four commands for each folder inc001, inc002, inc003 and inc004....

Don't know....

Last edited by bmxakias; 09-02-2019 at 10:18 AM.
 
Old 09-02-2019, 10:15 AM   #17
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
Quote:
Originally Posted by bmxakias View Post
Code:
while [[ -d "$INCDIR/inc$N" ]] ; do
    N=$(($N+1))
done

if ls -A $TARGETDIR/* > /dev/null 2>&1; then
    mkdir -p "$INCDIR/inc00$N"
    MYDIR="$INCDIR/inc00$N"
    touch $MYDIR/file.txt
fi
replace
Code:
  N=$(($N+1))
with
((N++))
 
Old 09-02-2019, 10:17 AM   #18
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,991

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
what happens whey you get to 10?
 
Old 09-02-2019, 10:22 AM   #19
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
exactly I was just thinking the 'same thing'. when you while loop finds last dir it stops incrementing at that number, then your next operation is to do something with a link and a number attached to it using $N, but mkdir *$N = last number found. ending with dir already present message.

or being that between you check without leading 00 and mkdir having leading 00. you know what you're doing over what I am speculating.

but Firerat might be referring to
009
0010
apposed to
009
010

Last edited by BW-userx; 09-02-2019 at 10:27 AM.
 
Old 09-02-2019, 10:34 AM   #20
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,991

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
Quote:
Originally Posted by BW-userx View Post
but Firerat might be referring to
009
0010
apposed to
009
010
bingo

edit, well
001
0010
0011
002
009

kinda messed up if you want order

Last edited by Firerat; 09-02-2019 at 10:36 AM.
 
Old 09-02-2019, 10:35 AM   #21
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled
After testing the code with 001 and ((N++)) the only working with no issues and up to 20 that i tested with no issues is this code:

Code:
while [[ -d "$INCDIR/inc$N" ]] ; do
    N=$((N++))
done

if ls -A $TARGETDIR/* > /dev/null 2>&1; then
    mkdir -p "$INCDIR/inc$N"
    MYDIR="$INCDIR/inc$N"
    touch $MYDIR/file.txt
fi
It creates the inc1 inc2 inc10 inc15 folders...

Now i am thinking to find a way to compare the input like:

check if user selected inc1 and if yes do this. If user selected inc2 do that and so on.... As there will not be more than 23 folders....


So maybe you can help me on that?

How to compare user input (use read to get it) with inc1 inc2 and so on ?

Thank you !

Last edited by bmxakias; 09-02-2019 at 10:39 AM.
 
Old 09-02-2019, 10:45 AM   #22
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
Quote:
Originally Posted by Firerat View Post
bingo

edit, well
001
0010
0011
002
009

kinda messed up if you want order
modified from something I found online
Code:
for i in `seq -w 1 100 | sed -n '1,100p'`; 
do 
    echo $i; 
done
001
002
003
004
005
006
007
008
009
010
011
012
....
100

Last edited by BW-userx; 09-02-2019 at 10:46 AM.
 
Old 09-02-2019, 10:51 AM   #23
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,991

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
Code:
mkdir -p "foo/bar/inc$( printf "%03d" ${N} )"
#edit I missed the 0 in %03d


Quote:
Originally Posted by bmxakias View Post
Can you please remove from your code the creation of the folders (as i do that from the backup script) and i already adjust it and now i have:

Code:
/backup/$DOMAIN/$DATE/database//inc/inc001
/backup/$DOMAIN/$DATE/database/inc/inc002
/backup/$DOMAIN/$DATE/database/inc/inc003

Also Ascending/Descending doesn't matter .... we may remove that also....

I just need to list folders and get the user input ...

With the above code i am getting the Ascending/Descending menu but i don't see anything listed to select and see if it works...

Thank you
did you pick 1 or 2?
you can skip that in the code

reverse order?

Code:
#select order in Ascending Descending;do
#    [[ "$order" ]] && $order && break 
#done

Descending

# see what I did?
have you tried the one in #8
https://www.linuxquestions.org/quest...8/#post6032085

Last edited by Firerat; 09-02-2019 at 10:52 AM.
 
Old 09-02-2019, 10:51 AM   #24
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled
I think that will work:

Code:
echo -e "Please type your inc folder name: \c"
read inc
if [[ $inc = "inc1" ]]
then
    echo good
else
    echo not good
fi
repeat that up to 23 ....

But how can i do that to avoid repeating this code 23 times?

Last edited by bmxakias; 09-02-2019 at 10:56 AM.
 
Old 09-02-2019, 10:51 AM   #25
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
Quote:
Originally Posted by bmxakias View Post
After testing the code with 001 and ((N++)) the only working with no issues and up to 20 that i tested with no issues is this code:

Code:
while [[ -d "$INCDIR/inc$N" ]] ; do
    N=$((N++))
done

if ls -A $TARGETDIR/* > /dev/null 2>&1; then
    mkdir -p "$INCDIR/inc$N"
    MYDIR="$INCDIR/inc$N"
    touch $MYDIR/file.txt
fi
It creates the inc1 inc2 inc10 inc15 folders...

Now i am thinking to find a way to compare the input like:

check if user selected inc1 and if yes do this. If user selected inc2 do that and so on.... As there will not be more than 23 folders....


So maybe you can help me on that?

How to compare user input (use read to get it) with inc1 inc2 and so on ?

Thank you !
If I go for solution, there will be two completely different methodologies being used here. It looks like you've taken to internal usage of VARs in here.

you can run script.
script gets list of dirs already created. puts them into an array, prints out results, ask user what dir he wants to back up ( I think that is whats going on here.) then using case statement find match then issue commands to back it up.

if you use functions you can incorporate flags to call the functions so your entire created and back up can be in the same script.

Last edited by BW-userx; 09-02-2019 at 10:57 AM.
 
Old 09-02-2019, 10:57 AM   #26
bmxakias
Member
 
Registered: Jan 2016
Posts: 164

Original Poster
Rep: Reputation: Disabled
Just edited my message above:


I think that will work also:

Code:
echo -e "Please type your inc folder name: \c"
read inc
if [[ $inc = "inc1" ]]
then
    echo good
else
    echo not good
fi
repeat that up to 23 ....

But how can i do that to avoid repeating this code 23 times?

What do you think about that?

Thank you

Last edited by bmxakias; 09-02-2019 at 10:59 AM.
 
Old 09-02-2019, 11:01 AM   #27
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
find /backupFolders > makealist
print list
ask user what number he wants
take that and go from there
 
Old 09-02-2019, 11:10 AM   #28
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,991

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
yeap


Regards the restore
what happens if one of the incremental backups is missing?

how are these inc. backups being made?

do you really need to restore 1 and 2 to get 3?

i.e., when you create backup inc1
that is a copy of original...

inc2 that is the difference between inc1 and the Current
inc3 that is the difference between inc1+inc2 and the Current

That is how I understand it based on the required restore inc1 then inc2 then inc3

Or, is each one a snapshot in time?


the former is more space efficient but more complicated, and risk of failure increases each backup ( since the last one depends on all the ones before it )

the later is far easier, but uses more space
 
Old 09-02-2019, 11:15 AM   #29
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
this is just an working example of known var / dir names
Code:
baseDir=$HOME/test
makeDir()
{
for i in `seq -w 1 100 | sed -n '1,100p'`; 
do 
    mkdir -p $baseDir/inc$i 
done
}
#touch $baseDir/dirlist
findDirs()
{
	find $baseDir -type d >> $baseDir/dirlist	
}

countDir()
{
	count=$(find $baseDir -type d -mindepth 1 -maxdepth 2 | wc -l)
}

#makeDir
findDirs
countDir
cat $baseDir/dirlist
echo              #removes leading space 
echo select inc001 - inc${count// }
creates 100 dir inc001 - inc100
gets count then using the known format shows amount up to last dir created to its 100 but removing the cat and only using the echo you'll get a sequence of start to last to pick from.

select inc001 - inc100
 
Old 09-02-2019, 11:38 AM   #30
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,991

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
Quote:
Originally Posted by BW-userx View Post
this is just an working example of known var / dir names
doesn't do the creation ( did that in other thread )


Code:
#!/bin/bash
Dir="${1%/}"
Dirs=() # intial array ${#Dirs[@]} == 0
for i in ${Dir}*;do
    [[ ${i} =~ ${Dir}[0-9]+ ]] && Dirs+=("${i}")
    ## checks if basename<numbers>
done


echo "Directory count = ${#Dir[@]}" # you want a count?
# no need to write files to disk ( which you need to clean up )
# no using find, bash globbed them
# 

select CHOICE in "${Dirs[@]}";do
    [[ "$CHOICE" ]] && break
done

for ((i=0;i <= $(( $REPLY - 1 ));i++));do
        echo restore "${Dirs[i]}"
done
simple
and no clean up
 
  


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
Bash Scripting Help, can't get values to compare seanferd Linux - General 5 01-08-2014 07:05 PM
[SOLVED] Bash scripting problem: CSV list of filenames and actual files compare; Issue with plus symbol gn000we Linux - General 2 08-05-2013 06:57 PM
bash scripting... taking user input and adding it to a particular area of a file... dagummit Programming 7 01-13-2010 12:07 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 02:20 PM

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

All times are GMT -5. The time now is 06:09 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