LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Basic bash script question (https://www.linuxquestions.org/questions/linux-newbie-8/basic-bash-script-question-372024/)

kevpatts 10-11-2005 04:13 PM

Basic bash script question
 
I have 4 folders in /Audio named:

Albums
Compilations
Singles & EPs
Audio Books

I'm looking for a bit of bash script that can create an files from these using the command something like:

touch ${DIRS[index]}.txt

Can anyone help?
Kev

zhangmaike 10-11-2005 04:52 PM

Code:

for x in /Audio/*
do
  test -d "$x" && touch "$x.txt"
done

or on one line:

Code:

for x in /Audio/*; do test -d "$x" && touch "$x.txt"; done
Will create one empty .txt file for each directory in /Audio.

...is that what you were asking?

kevpatts 10-12-2005 07:07 AM

Thanks,

That's close, but not quite it. I'm looking to put the values onto an array in such a way as the can be used many times. Also in such a way that when I use:
touch ${DIRS[index]}.txt

when the array entry is "Singles & EPs" I get "Singles & EPs.txt" instead of:

Singles.txt
&.txt
EPs.txt

That's what I currently get.

Thanks for the help.
Kev

zhangmaike 10-12-2005 01:02 PM

Code:

i=0

for x in /Audio/*
do
  test -d "$x" &&  DIRS[$i]="$x"
  i=$(($i+1))
done

Would load an array with all the directories in /Audio.

Because of the spaces in some of your directory names, you'll need to reference the array elements from within quotes like:

touch "${DIRS[index]}.txt"

to keep bash from splitting the array element at its spaces.

If this still isn't what you're asking for, maybe you could post your current code?


All times are GMT -5. The time now is 05:50 PM.