how do i begin with a script when testing if it don't exist

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
enhu
Posts: 302
Joined: Wed 27 May 2009, 02:13
Contact:

how do i begin with a script when testing if it don't exist?

#1 Post by enhu »

while [ the file don't exist ]; <----- how should this test be?
do
sleep
done

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Hello enhu.

It seems that your non-existent program will simply sleep forever? ;)

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#3 Post by MochiMoppel »

Compared to eternity 6 years is still a short time, but nevertheless it might be a good idea to let the script continue:

Code: Select all

while [ ! -e /path/to/file ]
do 
sleep 1
done
or

Code: Select all

until [ -e /path/to/file ]
do 
sleep 1
done

User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

#4 Post by drunkjedi »

Aha, musher doing a pelo act? Lol.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#5 Post by musher0 »

drunkjedi wrote:Aha, musher doing a pelo act? Lol.
Funny guy! :lol:

Not really!

I thought it resembled this script which I wrote in 2015 (?) to keep
updated the slocate databases of file names. Some of those lines I owe to
French member Médor, who in turn borrowed a few from our venerated
Puppy founder, BK.

This script actually sleeps 95 % of the time.

BFN.
~~~~~~~~~~~~~

Code: Select all

     1	#!/bin/ash
     2	# ash is used because of it is +/- 50 % faster than bash.
     3	# Filename: $SLOC/auto-updatedb-slocate.sh, # with link in /root/Startup.
     4	# Purpose: update the slocate database at every x interval of time.
     5	#
     6	# Thanks to French member Médor for having found the "while" time loop
     7	# below, and to member Uten for the 'nice' (pun intended! :-) tip.
     8	#
     9	# (c) Christian L'Écuyer (alias musher0), Gatineau (Qc), Canada, 12 & 19 févr. 2015, 14 oct. 2015.
    10	# Rév. 3 févr. et 28 févr. 2016. GPL2
    11	####
    12	export SLOC="/root/my-applications/SLocate" # variable
    13	sleep 4m # With this line, slocate will start to update its
    14	# db's exactly 4 minutes after your Puppy session has started.
    15	# After that run is finished, they'll be updated at the interval
    16	# you un-commented below (see lines 28 to 37).
    17	
    18	while [ 1 ]; do # This script updates all the slocate db's'
    19	# in the background, without imposing on your use of the computer.
    20		ionice -c 3 nice -n 19 $SLOC/Slocate-updt-all-no-text.sh &
    21	# ionice, etc. is from K. Lindsay's original /etc/cron.daily/slocate
    22	
    23	# We can take the opportunity to add here any number of other
    24	# commands that we want executed periodically in the background.
    25		sync;echo 3 > /proc/sys/vm/drop_caches
    26		wait
    27	# The above cleans the RAM caches and frees up RAM. *
    28	
    29	# Uncomment ONLY ONE LINE BELOW corresponding to your use (time interval).
    30	# sleep 20m # every 20 minutes # Use these short intervals only
    31	# sleep 30m # every 30 minutes # if you absolutely need to. The
    32	# sleep 45m # every 45 minutes # slocate DB process imposes a lot
    33	# sleep 1h # every hour ######## of wear on your drives.
    34	# sleep 90m # every hour and a half
    35	# sleep 2h # every 2 hours
    36	# sleep 150m # every 2 hours and a half
    37	sleep 3h # every 3 hours
    38	# sleep 210m # every 3 hours and a half.
    39	done
    40	
    41	####
    42	# * Derived from : auto_clean_mem, by Médor, 2015-02-08. Source :
    43	# http://murga-linux.com/puppy/viewtopic.php?p=826916&sort=lastpost#826916
    44	# Retrieved 09.02.2015 13:43:49. # According to Médor, he modeled
    45	# this loop on one used by BK on the Internet connection.
    46	#
    47	# Also thanks to Uten for his post at:
    48	# http://murga-linux.com/puppy/viewtopic.php?p=828580&sort=lastpost#828580
    49	# Retrieved 19.02.2015 14:24.
    50	####
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Pelo

not to create a new topic to answer a question already poste

#6 Post by Pelo »

I fully agree with Musher0 not to create a new topic to answer a question already posted..

User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

#7 Post by drunkjedi »

Don't take seriously Pelo, I was just jesting.

MochiMoppel, what should the script looks like if I want it to look for change in file or folder?

Let's say, I added few fonts, want to run update_fonts automatically.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#8 Post by MochiMoppel »

drunkjedi wrote:MochiMoppel, what should the script looks like if I want it to look for change in file or folder?
Completely different. No while loop, no sleep ... for periodical checking tasks you should use a cron job, not a permanently running script.

Not that I claim to understand what enhu asked for. The thread title makes no sense to me. I only assumed that his script halts until a non-existent file finally is created an then continues. At least it doesn't look as if he was asking for a permanent loop.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#9 Post by musher0 »

Code: Select all

#!/bin/ash
# CacheNewFont.sh
#
# For "drunkjedi"
#
# Goal: Recreate the font cache if we intalled 
# ----- a new font during the current hour.
# -------------------------- ========= ----
#
# Note -- This script is still "raw". It needs to be refined.
# ------- It is presented only for discussion purposes.
#
# musher0, 28 déc. 2016 
####
Hour="`date '+%H:'`" # Assuming 24 h clock on your system.
Day="`date '+%d:'`"

cd /usr/share/fonts/default/TTF

RecreatE () { [ "`ls -Algot *.ttf | awk '$1 ~ /-rw/ && $5 ~ /'$Day'/ && $6 ~ /'$Hour'/'`" ] && fc-cache -r || echo -e "\n\t\tNo need.\n"; }

RecreatE # Presented as a function because we might want to repeat 
# it at a certain interval. For ex.:
# while [ 1 ];do
# RecreatE
# sleep 1h
# done
Attachments
CacheNewFont.jpg
(10.47 KiB) Downloaded 170 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

#10 Post by drunkjedi »

I found inotify-tools in Fatdog, I don't know if other puppies have it.

Code: Select all

inotifywait -m /usr/share/fonts/default/TTF -e create -e moved_to
Can be used to detect if a file is moved to or is created in a directory....

Exit status returned by inotifywait

Code: Select all

Exit status:
	0  -  An event you asked to watch for was received.
	1  -  An event you did not ask to watch for was received
	      (usually delete_self or unmount), or some error occurred.
	2  -  The --timeout option was given and no events occurred
	      in the specified interval of time.
I will test this in few min..

User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

#11 Post by drunkjedi »

Ok I don't know why this script doesn't work

Code: Select all

while inotifywait -m -e create,moved_to /mnt/sda8/test/xyz; do exec /mnt/sda8/test/Script; done
While this does

Code: Select all

while inotifywait -e create,moved_to /mnt/sda8/test/xyz; do exec /mnt/sda8/test/Script; done

Code: Select all

	-m |--monitor  	Keep listening for events forever or until --timeout expires.
	              	Without this option, inotifywait will exit after one event is received.
Maybe I should try piping the ouput of inotifywait -m.

User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

#12 Post by drunkjedi »

Ok this works

Code: Select all

inotifywait -m /mnt/sda8/test/xyz -e create -e moved_to | 
	while read a; 
		do /mnt/sda8/test/Script
done
Every time a file is made in the folder /mnt/sda8/test/xyz, the script /mnt/sda8/test/Script gets run.
Which was just a xmessage.

Is this the right way?

User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

#13 Post by drunkjedi »

Or should I echo it's exit status and run the script if it's 0?

Post Reply