LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 07-16-2017, 05:38 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
how to log an execution within a bash script?


How do I get the return value being the path/to/imageName.jpg
into a log file using bash script?

this is just part of the entire script, but within this function is how the image gets set to background.

I am trying to figure out how to get into a file along with running at the same time when executed.

because some how after all of that execution of commands a path and image name gets returned in order for the app to display it on the screen as a background. (using mhsetroot).

I want to get that into a file so I can check its actual randomnisisisis

the code has been bold'ed

Code:
startChanging()
{

while true 
do
     SetSubDir=$(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
	    
	 WALLPAPERS="$WALLPAPERS/$SetSubDir"
	 
	 setShow=$(shuf -i 1-5 -n 1)
		
	case "$setShow" in
	1)
	  Imageis="-fill"
	;;
	2)
	  ImgNum1
	  Imageis="-dtile $ImageDimentions"
	  #Imageis="-dtile 300x300"
	;;
	3)
	  Imageis="-fimgv"
	;;
	4)
	  Imageis="-dcenter $((RANDOM % 100 + 500  | bc ))"x"$((RANDOM % 300 + 800 | bc ))"
	;;
	5)
		Imageis="-fimgd"
	;;
	esac
		
	gradientAngle=$((gradientAngle+=10))
	
	[[ $gradientAngle -gt '160' ]] && gradientAngle=1
	
echo   "$(find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z | xargs -0 \
	  mhsetroot -add $(color) -add $(color) -gradient $gradientAngle $Imageis)" >> changebgimage.log
	
	#find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z | xargs -0 \
	#mhsetroot -addd $(color) 3 -addd $(color2) 5 -addd $(color) 3 -addd $(tint) 8 -gradient "$(setGradient)" $setI $ImageDimentions
	 
	#mhsetroot -solid $(echo \#$(head -n 1 /dev/urandom | od -t x1 -N 9 | awk '{print $2 $3 $4}'))
  sleep  1m
  done &
}
KillPid

startChanging
This does not even get the path/FileName.pjg eitehr
Code:
SetImage=$(find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z | xargs -0 \
	  mhsetroot -add $(color) -add $(color) -gradient $gradientAngle $Imageis)
	  
	  
	  echo "$SetImage"
set -x says

Code:
++++ od -t x1 -N 9
++++ awk '{print $2 $3 $4}'
+++ echo '#6c3b7d'
++ xargs -0 mhsetroot -add '#3bbb33' -add '#6c3b7d' -gradient 10 -dtile 216x486
+ SetImage=
+ echo ''

+ sleep 1m
an Image still gets set to the background.

Last edited by BW-userx; 07-16-2017 at 06:11 PM.
 
Old 07-16-2017, 06:54 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
The last thing in your pipe chain is mhsetroot so your redirect, or SetImage=$(...) assignment, will only receive whatever output mhsetroot produces, if any.

Downstream of the find which produces the filenames, or just before the mhsetroot pipe, add this...

Code:
... | tee -a changebgimage.log | ...

Last edited by astrogeek; 07-16-2017 at 06:56 PM.
 
Old 07-17-2017, 08:59 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by astrogeek View Post
The last thing in your pipe chain is mhsetroot so your redirect, or SetImage=$(...) assignment, will only receive whatever output mhsetroot produces, if any.

Downstream of the find which produces the filenames, or just before the mhsetroot pipe, add this...

Code:
... | tee -a changebgimage.log | ...
Ok, that worked but, I am getting a / slash added to the path each time it is called and this Image included, to explain it.
Code:
  find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z  | tee -a changebgimage.log | xargs -0  \
	  mhsetroot -add $(color) -add $(color) -gradient $gradientAngle $Imageis
	  #add a new line after tee adds entry to log file
	  echo "" >> changebgimage.log
in that image I was getting this "error" before adding that new line using echo.

I do not know why I am getting / slashes added to each file every time it is called to set background. Details in attached image.

Invalid char I think is that little do hicky that tee is putting on the end or whatever is placing that little do hicky at the end of the file name.
Attached Thumbnails
Click image for larger version

Name:	Screenshot_IOS-8859-1-2017-07-17_08-52-46.png
Views:	7
Size:	55.6 KB
ID:	25486  

Last edited by BW-userx; 07-17-2017 at 09:02 AM.
 
Old 07-17-2017, 12:10 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
latest addtion to code but still getting that issue

Code:
	
	if [[ $(xprop -root -notype _NET_CURRENT_DESKTOP) -eq '1' ]] ||  [[ $(xprop -root -notype _NET_CURRENT_DESKTOP) -eq '3' ]] ; then
	
	  find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z  | tee -a changebgimage.log | xargs -0  \
	  mhsetroot -add $(color) -add $(color) -gradient $gradientAngle $Imageis
	  #add a new line after tee adds entry to log file
	  echo "" >> changebgimage.log
	  
	  else
	  echo "kill app" >> "$appname.log"
	  pkill $appname
	  pkill sleep
	  fi
 
Old 07-17-2017, 08:11 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
I do not see anything obvious that might result in the extra '///'s, but I don't think this is doing what you think it is...

Code:
shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z
If the find produced any result and wc returnd the linecount, you would end up with zero or a negative number - IF the math worked, but it doesn't!

In order to produce a numeric argument, that would need to be...

Code:
...$((1-$(find pics/ -type f |wc -l)))
...but that would still produce a zero or negative result. Also, since the arguments to find differ from the find which produces the list, the count would still be different than what comes down the pipe.

I am not sure I understand what you are trying to do there, but if you want a single file randomly selected then this should do it in that part of the pipe...

Code:
 ...| shuf -n1 -z |...
As the shuf preceeds the tee, I would fix that first and see if it fixed the other issue.

It might also be helpful for you to work with each part of you long pipe separately first, to be sure each does what you think, then tie them all together.

Hope that helps!

Last edited by astrogeek; 07-17-2017 at 08:15 PM. Reason: typos, typos...
 
Old 07-17-2017, 08:44 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by astrogeek View Post
I do not see anything obvious that might result in the extra '///'s, but I don't think this is doing what you think it is...

Code:
shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z
If the find produced any result and wc returnd the linecount, you would end up with zero or a negative number - IF the math worked, but it doesn't!

In order to produce a numeric argument, that would need to be...

Code:
...$((1-$(find pics/ -type f |wc -l)))
...but that would still produce a zero or negative result. Also, since the arguments to find differ from the find which produces the list, the count would still be different than what comes down the pipe.

I am not sure I understand what you are trying to do there, but if you want a single file randomly selected then this should do it in that part of the pipe...

Code:
 ...| shuf -n1 -z |...
As the shuf preceeds the tee, I would fix that first and see if it fixed the other issue.

It might also be helpful for you to work with each part of you long pipe separately first, to be sure each does what you think, then tie them all together.

Hope that helps!
the first time in script here
Code:
  SetSubDir=$(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
example
Code:
userx%slackwhere ⚡ imaging ⚡> WALLPAPERS=/media/data/wallpaper                                                                                                                                                                                                             
userx%slackwhere ⚡ imaging ⚡> SetSubDir=$(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
userx%slackwhere ⚡ imaging ⚡> echo $SetSubDir
453
returns a number within the subdirectory wallpaper. the subdirectories are all numbers. 1 thru N
this way regardless of how many subdirectories it will give a random number between 1 and last (highest numbered) subdirectory.

next line

Code:
WALLPAPERS="$WALLPAPERS/$SetSubDir"
resets WALLPAPER to path to parent dir plus subdir (number)

next to get a random image within that subdir 1 thru total number of images within that subdir.

Code:
      -n  1 thru highest number of files within subdir -z, --zero-terminated
shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z
that entire line
Code:
userx%slackwhere ⚡ imaging ⚡> find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z  | tee -a changebgimage.log | xargs -0
/media/data/wallpaper/2/DispicableMe-1-99824.png
userx%slackwhere ⚡ imaging ⚡>
returns an image randomly.

from the partent/subdir
Code:
userx%slackwhere ⚡ imaging ⚡> WALLPAPERS=/media/data/wallpaper/2                                                                                                                                                                                                           
userx%slackwhere ⚡ imaging ⚡> echo $(find "$WALLPAPERS" -type f | wc -l)
110
userx%slackwhere ⚡ imaging ⚡> find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z  | tee -a changebgimage.log | xargs -0                                                         
/media/data/wallpaper/2/Images.Image-188-176.jpg
userx%slackwhere ⚡ imaging ⚡>

Last edited by BW-userx; 07-17-2017 at 08:46 PM.
 
Old 07-18-2017, 04:25 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
You ought to simplify those overcomplicated commands, that will help you to understand what's happening and will allow also to follow intermediate steps/values.
echo $(command....) is almost completely equivalent to command.... but costs more.
 
Old 07-18-2017, 09:21 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by pan64 View Post
You ought to simplify those overcomplicated commands, that will help you to understand what's happening and will allow also to follow intermediate steps/values.
echo $(command....) is almost completely equivalent to command.... but costs more.
Code:
+ echo 'find /media/data/wallpaper/307 -type f \( -name '\''*.jpg'\'' -o -name '\''*.png'\'' -o -name *.bmp \) -print0 | shuf -n 1-44 -z  | tee -a changebgimage.log  | xargs -0          mhsetroot -add #84b996 -add #2cae59 -gradient 10 -fimgv)'
find /media/data/wallpaper/307 -type f \( -name '*.jpg' -o -name '*.png' -o -name *.bmp \) -print0 | shuf -n 1-44 -z  | tee -a changebgimage.log  | xargs -0      mhsetroot -add #84b996 -add #2cae59 -gradient 10 -fimgv)
or echo this is off cli without set -x
Code:
userx%slackwhere ⚡ ~ ⚡> find /media/data/wallpaper/778 -type f \( -name '*.jpg' -o -name '*.png' -o -name *.bmp \) -print0 | shuf -n 1-10 -z  | tee -a changebgimage.log  | xargs -0 mhsetroot -add #d4fb95 -add #d98d24 -gradient 10 -dcenter 533x811
to break that up and echo it separately -- I don't think it'd produce what desired results to try and see what is going on either.


as far as that end character tee is putting on it I do not know what or why.

in emacs it show this, I cannot copy it because it is read only so I will simulate it with my fancy keyboard.

Code:
/media/data/wallpaper/609/////////////////Image-270-0555.png^@
Kwrite shows something that looks like a mini % sign at the end.

this is rather simple whence you get your head wrapped around it. Plus as I was writing that out I realized I was out of time so I posted and ran out to catch the last bus home.

let me break it down for you.

Code:
wc -l
returns what when piped into ls or find?
Code:
find /home -type d | wc -l
find: `/home/surooterdude': Permission denied
find: `/home/lost+found': Permission denied
3709
shows a number value of total dir in /home - minus the ones it cannot look at.

Using shuf
Code:
shuf -i 1-10 -n 1                                                                    
9
-i indicates a range 0 ... whatever N ; -n 1 indicates return one number within that range RANDOM. so it is simple math when we put these two methods together.

if one wants to RANDOMize the obtaining of one image within many subdirectories then what is the best way to get a random? by randomizing everything posable, because RANDOM is not RANDOM.


get count of total subdir in wallpaper parent dir, feed it into shuf for HI end.

what is the fastest and simplest way to do that?
Code:
SetSubDir=$(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
Look closely and see that all this is doing is returning total count of dir and subdir in path $WALLPAPERS minus 1 to remove the parent dir count. then tail end is -n 1 return one of them numbers from the RANGE= 1-$(total subDir) - n 1.

then assign that same subdir to the entire path for find a random image to display on the background. JUst add the subDir
Code:
 WALLPAPERS="$WALLPAPERS/$SetSubDir"
inside it reads /media/data/wallpaper/(whatever number is returned) because all of the subdirectories are numbered 1 thru N

Now just apply that same line of thought to get a RANDOM image
Code:
 this sets the type of files to look for                  print0  uses a null character as a separator                       
 find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z  | tee -a changebgimage.log | xargs -0  \
	  mhsetroot -add $(color) -add $(color) -gradient $gradientAngle $Imageis
break it down a bit
first just find file types needed then place a NULL at the end with -print0 so the program taking in the file name will not error.
https://linux.die.net/man/1/xargs
Code:
userx%slackwhere ⚡ ~ ⚡> find /media/data/wallpaper/2 -type f -name "*.png"   -print0                                                                                                                                                             
/media/data/wallpaper/2/1920x1080.png/media/data/wallpaper/2/DispicableMe-1-104124.png/media/data/wallpaper/2/DispicableMe-1-10624.png/media/data/wallpaper/2/DispicableMe-1-108024.png/media/data/wallpaper/2/DispicableMe-1-108124.png/media/data/wallpaper/2/DispicableMe-1-110024.png/media/data/wallpaper/2/DispicableMe-1-116024.png/media/data/wallpaper/2/DispicableMe-1-119724.png/media/data/wallpaper/2/DispicableMe-1-121624.png/media/data/wallpaper/2/DispicableMe-1-123424.png/media/data/wallpaper/2/DispicableMe-1-132824.png/media/data/wallpaper/2/DispicableMe-1-23324.png/media/data/wallpaper/2/DispicableMe-1-29124.png/media/data/wallpaper/2/DispicableMe-1-33224.png/media/data/wallpaper/2/DispicableMe-1-33924.png/media/data/wallpaper/2/DispicableMe-1-37224.png/media/data/wallpaper/2/DispicableMe-1-41324.png/media/data/wallpaper/2/DispicableMe-1-44524.png/media/data/wallpaper/2/DispicableMe-1-50724.png/media/data/wallpaper/2/DispicableMe-1-55724.png/media/data/wallpaper/2/DispicableMe-1-60624.png/media/data/wallpaper/2/DispicableMe-1-79224.png/media/data/wallpaper/2/DispicableMe-1-84824.png/media/data/wallpaper/2/DispicableMe-1-99824.png/media/data/wallpaper/2/Heidy-Pino-269-0006.png/media/data/wallpaper/2/Heidy-Pino-269-12066.png/media/data/wallpaper/2/Heidy-Pino-269-3996.png/media/data/wallpaper/2/Heidy-Pino-269-8036.png/media/data/wallpaper/2/slack-blulin-1600x1200.png/media/data/wallpaper/2/slack-glass-1024x768.png/media/data/wallpaper/2/DispicableMe-1-112224.png/media/data/wallpaper/2/DispicableMe-1-60224.png/media/data/wallpaper/2/Nature.Image-63-63.pnguserx%slackwhere ⚡ ~ ⚡>
now pipe all of that into shuf and give it a range to pick from. the HI end matches the total files - this is kept true by using that same method beforehand
Code:
find /media/data/wallpaper/2 -type f -name "*.png" -print0 |shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z 
/media/data/wallpaper/2/DispicableMe-1-10624.png
then pipe it to xargs -0 (Input items are terminated by a null character) NULL character is gained by -print0 before hand.

https://linux.die.net/man/1/xargs

the only thing I just found while picking this apart and explaining it to you is this within shuf
https://linux.die.net/man/1/shuf

Code:
shuf -n 1-HI -z
when it use to be
Code:
return one
shuf -n 1 -z
then I changed it to
Code:
          LOW end = 1 to Hi end = total files in subdir set in code prior to 
shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z
not changing or forgetting to change the -n to -i
but when I ran and run it on the cli I get a image file returned.
now that I have changed that -n to an -i I get numbers returned instead.
Code:
userx%slackwhere ⚡ ~ ⚡> find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -i 1-$(find "$WALLPAPERS" -type f | wc -l) -z     
228789558108867390608280626820256133037721683842410639454071810033657665956962685711431431112156593499842388941705481619444351112299419746310129110976491468821527951105233385109344869677553927717103764719102871041073227555010userx%slackwhere ⚡ ~ ⚡>
so why that is returning only one actual file and path to it being formed improperly I do not know.
Code:
userx%slackwhere ⚡ ~ ⚡> find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z 
/media/data/wallpaper/1/Abstract 3.jpguserx%slackwhere ⚡ ~ ⚡>
it worked so I ran with it.
.. so let me put this on hold for a bit - remove sleep and run it with various changes to code and see what happens
Code:
shuf -n 1 -z
and see if it stops doing this.
Code:
/media/data/wallpaper/117///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Images.Image-141-129.jpg
/media/data/wallpaper/117////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////slack-freedom-1920x1200.jpg
/media/data/wallpaper/117/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////t137.jpg

---------------------------------------------------------

as far as that end character tee is putting on it I do not know what or why.

in emacs it show this, I cannot copy it because it is read only so I will simulate it with my fancy keyboard.

Code:
/media/data/wallpaper/609/////////////////Image-270-0555.png^@
Kwrite shows something that looks like a mini % sign at the end.

(that was a repeat so it wouldn't get lost in the shuffle)

Last edited by BW-userx; 07-18-2017 at 09:57 AM.
 
Old 07-18-2017, 10:07 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Stick a webcam on the gallows or the electric chair, and use a bash script to control it?

Uhh, sorry.
 
Old 07-18-2017, 11:09 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
OK if you got through all of that explanation of how that code is working : now one gets to read this.

grab a coffee if you plan on reading through this one to get an idea of what might be going on here, because the code is good!
Just some how it is getting thrown off.

I ran it again off cli.

Example 1:
using this to get random subdirectories, then random file within that set subdirectory.
Code:
 SetSubDir=$(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
 WALLPAPERS="$WALLPAPERS/$SetSubDir"
results are:
Code:
/media/data/wallpaper/298/DispicableMe-1-121524.png
/media/data/wallpaper/157/2013-04-02 12.57.05.jpg
/media/data/wallpaper/133/DispicableMe-1-129824.png
/media/data/wallpaper/133/Rocks.jpg
/media/data/wallpaper/133//DispicableMe-1-26324.png
/media/data/wallpaper/133///Gothic.Image-52-51.jpg
/media/data/wallpaper/133////Gothic.Image-52-51.jpg
/media/data/wallpaper/133/////t065.jpg
/media/data/wallpaper/133//////Images.Image-211-199.jpg
/media/data/wallpaper/133///////DispicableMe-1-47224.png
/media/data/wallpaper/133////////Scary.Movies.Image-9-8.jpg
/media/data/wallpaper/133/////////C200.jpg
/media/data/wallpaper/133//////////DispicableMe-1-47224.png
/media/data/wallpaper/133///////////Rocks.jpg
/media/data/wallpaper/133////////////DispicableMe-1-129824.png
/media/data/wallpaper/133/////////////C200.jpg

Example 2:
changed code - removed
Code:
 SetSubDir=$(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
	    
	# WALLPAPERS="$WALLPAPERS/$SetSubDir"
so only this worked using the Parent dir wallpaper and all of its subdirectories
Code:
 find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' -o -name "*.bmp" \) -print0 | shuf -n 1 -z  | tee -a changebgimage.log  | xargs -0 mhsetroot -add $(color) -add $(color) -gradient $gradientAngle $Imageis
giving me this:


Code:
/media/data/wallpaper/140/2013-04-25 07.42.01.jpg
/media/data/wallpaper/12/t277.jpg
/media/data/wallpaper/204/slackware_fts2.jpg
/media/data/wallpaper/77/t320.jpg
/media/data/wallpaper/170/yvjw004m8kk7.jpg
/media/data/wallpaper/215/t166.jpg
/media/data/wallpaper/204/Scary.Movies.Image-26-25.jpg
/media/data/wallpaper/163/DispicableMe-1-63824.png
/media/data/wallpaper/59/C381.jpg
/media/data/wallpaper/82/Images.Image-541-529.jpg
/media/data/wallpaper/241/Images.Image-776-764.jpg
/media/data/wallpaper/152/DispicableMe-1-51624.png
/media/data/wallpaper/43/DispicableMe-1-89024.png
/media/data/wallpaper/201/C237.jpg
/media/data/wallpaper/59/DispicableMe-1-62024.png
/media/data/wallpaper/265/DispicableMe-1-66224.png
/media/data/wallpaper/101/slack-baghira-1024x768.jpg
/media/data/wallpaper/298/t385.jpg
/media/data/wallpaper/114/Scary.Movies.Image-65-64.jpg
Example 3:

changing that shuf back to
Code:
shuf -n 1-$(find "$WALLPAPERS" -type f | wc -l) -z
keeping
Code:
 SetSubDir=$(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
out of the picture
Results:
Code:
/media/data/wallpaper/185/Nature.Image-106-106.png
/media/data/wallpaper/82/Scary.Movies.Image-95-94.jpg
/media/data/wallpaper/82/Nature.Image-51-51.jpg
/media/data/wallpaper/275/Yosemite.jpg
/media/data/wallpaper/51/C135.jpg
/media/data/wallpaper/267/HD.Women.Image-244-243.jpg
/media/data/wallpaper/129/Gothic.Image-49-48.jpg
/media/data/wallpaper/128/slack-greenwhite-958x766.jpg
/media/data/wallpaper/13/DispicableMe-1-131424.png
/media/data/wallpaper/210/Gothic.Image-34-33.jpg
/media/data/wallpaper/105/Images.Image-31-19.jpg
/media/data/wallpaper/223/C065.jpg
/media/data/wallpaper/41/DispicableMe-1-99024.png
/media/data/wallpaper/260/C308.jpg
/media/data/wallpaper/194/HD.Women.Image-45-44.jpg
/media/data/wallpaper/284/slack-blue3-1280x800.jpg
/media/data/wallpaper/254/Nature.Image-64-64.png
/media/data/wallpaper/104/C260.jpg
/media/data/wallpaper/17/dmarcu_romania.jpg
/media/data/wallpaper/55/C139.jpg
/media/data/wallpaper/130/slack-grey1-1365x1024.png
the looks of it are when ever it stays within the same subdirectory it will add a slash to it each time it uses it in succession. example 1.

so this code to get a random subdir is not random much if any. always returning a same number each time.

Code:
SetSubDir=$(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
where the actual value on HI end is as it stand right now during testing is 300 giving it
Code:
shuf -i 1-(301-1) -n 1
seeing this effect on the command line where subdirctories = 300
Code:
userx%slackwhere ⚡ ~ ⚡> WALLPAPERS=/media/data/wallpaper
userx%slackwhere ⚡ ~ ⚡> echo $(find "$WALLPAPERS" -type d | wc -l)
301
return is 301 due to parent 'wallpaper' dir.

to remove the 1 to eliminate the parent directory count. do the math
Code:
userx%slackwhere ⚡ ~ ⚡> echo $((($(find "$WALLPAPERS" -type d  | wc -l)-1)))
300
adding that bit of code back into the mix I get crap results.
Code:
   SetSubDir=$(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
     
  preSubDir=$((($(find "$WALLPAPERS" -type d  | wc -l)-1)))

  preSubDirType2=$(find "$WALLPAPERS" -type d  | wc -l)

     echo "preSubDir: $preSubDir" >> changebgimage.log
     echo "" >> changebgimage.log
     echo "preSubDirType2: $preSubDirType2" >> changebgimage.log
     echo "" >> changebgimage.log
     echo "SetSubDir: $SetSubDir" >> changebgimage.log

     WALLPAPERS="$WALLPAPERS/$SetSubDir"
results are:
Code:
preSubDir: 300

preSubDirType2: 301

SetSubDir: 157

preSubDir: /media/data/wallpaper/300

preSubDirType2: /media/data/wallpaper/301


SetSubDir: /media/data/wallpaper/157

/media/data/wallpaper/157/2013-04-27 19.13.42.jpg

# now it starts returning crazy bogus numbers. When the code is good! it works in the CLI. 

preSubDir: 0

preSubDirType2: 1

SetSubDir: 

preSubDir: /media/data/wallpaper/157/0

preSubDirType2: /media/data/wallpaper/157/1


SetSubDir: /media/data/wallpaper/157/

/media/data/wallpaper/157/2013-04-27 19.11.01.jpg
where it is working then it starts to error out, it is then when it keeps using the same subdir more than once it starts adding slashes to the path to image.

when using this same code on the CLI it is always good returns.
Code:
userx%slackwhere ⚡ ~ ⚡> WALLPAPERS=/media/data/wallpaper

userx%slackwhere ⚡ ~ ⚡> echo $(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)                                                                                                                                                       
4
userx%slackwhere ⚡ ~ ⚡> echo $(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
13
userx%slackwhere ⚡ ~ ⚡> echo $(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
233
userx%slackwhere ⚡ ~ ⚡> echo $(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
28
userx%slackwhere ⚡ ~ ⚡> echo $(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
204
userx%slackwhere ⚡ ~ ⚡> echo $(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
119
userx%slackwhere ⚡ ~ ⚡> echo $(shuf -i 1-$((($(find "$WALLPAPERS" -type d  | wc -l)-1))) -n 1)
205

still does not explain the crap at the end when tee splits it off into that log file.

when I got to run it through a different script to get the results just so I can copy paste it in here.

Code:
#!/bin/bash

# july 17 2017
# strip log file

pfile=$1

while IFS= read -r file
do
echo "$file" >> BadlyAddingSlachesAndCrapAtEndOfImageSentFromTee-aToLogFile
done < "$pfile"
 
  


Reply



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
Execution of the bash script is too Slower. sunil21oct Linux - Newbie 17 04-27-2017 05:59 PM
how to capture or log outputs/message during script execution VijayaRaghavanLakshman Linux - Newbie 7 01-25-2012 08:00 AM
Bash script execution dann_radkov Linux - Newbie 5 10-18-2011 09:46 AM
[SOLVED] Bash script execution error dann_radkov Linux - Newbie 4 09-13-2011 03:15 PM
BASH: open console on script execution Quis Programming 2 02-07-2006 09:41 AM

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

All times are GMT -5. The time now is 11:04 AM.

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
Open Source Consulting | Domain Registration