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 09-02-2019, 11:42 AM   #31
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,079

Rep: Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903

Quote:
Originally Posted by Firerat View Post
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
so just disregard that part of it, and take the relevant part. The point I was showing is get the total amount already present then offer that to the user to remove the guess work.
 
1 members found this post helpful.
Old 09-02-2019, 11:59 AM   #32
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,994

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
basic dir creation

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

mkdir -vp "${Dir}$( printf "%03d" $(( ${#Dirs[@]} + 1 )) )"
where is fails is when there is a gap ( or gaps )
1, 2, 4, 5
recreates 5

we could check the array with something like this

Code:
foo=( "inc001" "inc002" "inc004" )
for ((i=0;i<${#foo[@]};i++));do
    [[ inc$( printf "%03d" $((i+1)) ) == ${foo[i]} ]] \
       && echo ${foo[i]} is good \
       || echo ${foo[i]} is bad
done
 
1 members found this post helpful.
  


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 08:16 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