struggling with cycling thru list - bash

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
bulsatar
Level 2
Level 2
Posts: 99
Joined: Sun May 26, 2013 1:05 am
Contact:

struggling with cycling thru list - bash

Post by bulsatar »

Morning Everyone,

Found a bash script that I wanted to alter as it wasn't working correctly but don't know enough to get it working and have been fumbling around the net with no luck.

The original bit below outputs the entire list in one got and does not iterate thru each item:

Code: Select all

startFile="$(ls -1 "${wallpaperDir}" | egrep -i "\.(png|jpg|jpeg|bmp|gif)$" | head -n 1)"
endFile="$(ls -1 "${wallpaperDir}" | egrep -i "\.(png|jpg|jpeg|bmp|gif)$" | tail -n 1)"

for line in "$(ls -1 "${wallpaperDir}" | egrep -i "\.(png|jpg|jpeg|bmp|gif)$")"; do
if [ $(echo "${line}" | grep -c "$endFile") -eq "1" ]; then
	nextFile=$startFile
else
	nextFile="$(ls -1 "${wallpaperDir}" | egrep -i "\.(png|jpg|jpeg|bmp|gif)$" | grep -A 1 "${line}" | tail -n 1)"
fi
echo "
 <static>
  <duration>${timeDisplay}</duration>
  <file>${wallpaperDir}/${line}</file>
 </static>
 <transition>
  <duration>${timeTransition}</duration>
  <from>${wallpaperDir}/${line}</from>
  <to>${wallpaperDir}/$nextFile</to>
 </transition>" >> "${wallpaperDir}/background.xml"
echo -n "."
done
So I tried to move the list to an array object and iterate thru that instead but keep getting different errors (mainly because bash confuses the hell out of me and I have no idea what I am doing even after looking for hours at examples). Error happens when trying to setup the array

Code: Select all

filearray="(ls -1 "${Dir}" | egrep -i "\.(png|jpg|jpeg|bmp|gif)$")"

for line in "${filearray[@]}"; do
Thanks for any assistance, guidance, prayers....an explanation would be appreciated as I would like to wrap a little of my brain around bash and grep. Or even better if there was a good ide out there that used "normal" language to create a bash script would be great!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
bulsatar
Level 2
Level 2
Posts: 99
Joined: Sun May 26, 2013 1:05 am
Contact:

Re: struggling with cycling thru list - bash

Post by bulsatar »

nevermind. quit trying to use grep and made my life much easier!! Here is the link for the code I eventually got working: http://forums.linuxmint.com/viewtopic.php?f=42&t=168953
Locked

Return to “Scripts & Bash”