How to sort pictures from newest to oldest? - image

I have this script. It works by taking all the pictures in a folder, and make a webpage of it. Oldest picture on top, and newer pictures down the page.
How can I revert the way the pictures are displayed? I want the newest on top.
<?php
$folder = 'ute/grabs/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
echo '<table>';
for ($i = 0; $i < $count; $i++) {
echo '<tr><td>';
echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a>';
echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));
echo '</td></tr>';
}
echo '</table>';
?>

Put this after glob.
usort($files, create_function('$b,$a', 'return filemtime($a) - filemtime($b);'));
See: glob() - sort by date
Note: I have changed the first argument of the "create_function" to reverse the order.

Thank you Icarus3
That worked great.
This was the final solution:
<?php
$folder = 'webcam/webcam/ute/grabs/';
$filetype = '*.jpg';
$files = glob($folder.$filetype);
usort($files, create_function('$b,$a', 'return filemtime($a) - filemtime($b);'));
$count = count($files);
echo '<table>';
for ($i = 0; $i < $count; $i++) {
echo '<tr><td>';
echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a><br>';
echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));
echo '</td></tr>';
}
echo '</table>';
?>

Related

Powershell loop values of multiple variable in array

Trying to output multiple variables here in one and then email.
I keep getting one for obvious reason that its not storing all values.
Can you please help?
foreach ($computer in $computer_list)
{
# Write-Host $computer
$file = Get-childItem "\\$computer\c$\Users\test.log" -Include *log
$size = $file.Length/1gb
for ($i=0;$i -lt $computer.ReadCount; $i++)
{
$body += "$computer"
$body += "$file"
$body += "$size"
}

Unix file making with one randomly chosen file in a directory

I’m having a hard time figuring out why my script will make files, but not add the text into the files it creates in the “eggs” directory. Can anyone help me figure out what I have wrong in my code? Or offer suggestions?
I’ve tried single > and double >> for the text appending to the file but it doesn’t. It just leaves the files blank.
Edit:
file=0
RandomEgg=$(( RANDOM % 10 ))
cd eggs
while [ $file -lt 10 ]
do
touch "egg$file"
file=$(( file +1 ))
done
for files in $(ls eggs)
do
if [ $file -eq $RandomEgg ]
then
echo 'Found it!' > egg$file
else
echo 'Not Here!' > egg$file
fi
done
In bash, the script could be reduced into
cd eggs || exit
RandomEgg=$(( RANDOM % 10 ))
for ((i = 0; i < 10; ++i)); do
if ((i == RandomEgg)); then
echo 'Found it!'
else
echo 'Not Here!'
fi > egg$i
done
or,
cd eggs || exit
RandomEgg=$(( RANDOM % 10 ))
msg=('Not Here!' 'Found it!')
for ((i = 0; i < 10; ++i)); do echo "${msg[i == RandomEgg]}" > egg$i; done
Change the second loop to:
for files in egg*
do
if [ $files = "egg$RandomEgg" ]
then
echo 'Found it!' > $files
else
echo 'Not Here!' > $files
fi
done
You don't need to use ls to list the files, just use a wildcard. You were also listing the wrong directory -- the files were created in the current directory, not the eggs subdirectory.
You need to use $files as the filename, not egg$file, since that's the variable from this for loop.
You must use = to compare strings, not -eq. -eq is for numbers.

Having problems to substract a variable from $end_date in #bash

I am trying to substract the variable $i from the $end_date variable - any advice?
#!/bin/bash
COUNT="5"
declare -a arrWANTEDBACKUPS;
for ((i = 0 ; i < $COUNT ; i++)); do
WANTEDBACKUPNAME=`date '+%Y%m%d_%H00' -d "$end_date-$i hours"`;
arrWANTEDBACKUPS=(${arrWANTEDBACKUPS[#]} "$WANTEDBACKUPNAME");
echo "$arrWANTEDBACKUPS[$i]";
echo "Test";
done
To do math with bash you need double round brackets, like in this example:
echo "$(($end_date-$i)) hours"
4 hours
So this works for me:
date '+%Y%m%d_%H00' -d "$(($end_date-$i)) hours"
20200901_1900
#Léa Gris & #TheSlater
Thanks - got it!
Here my solution:
#!/bin/bash
COUNT="5"
declare -a arrWANTEDBACKUPS;
for ((i = 0 ; i < COUNT ; i++)); do
WANTEDBACKUPNAME=$(date '+%Y%m%d_%H00' -d "-$i hours");
arrWANTEDBACKUPS=(${arrWANTEDBACKUPS[#]} "$WANTEDBACKUPNAME");
echo "${arrWANTEDBACKUPS[$i]}";
echo "$WANTEDBACKUPNAME";
echo "Test";
done

How to assign multiple string values into a variable in shell scripts?

I'm trying to loop a text file changing the number and the hole string is store into a shell variable, to print it latter
I've tried echo the string an the integer (number from the loop) using ">>"
#!/bin/bash
a=0
while [ "$a" -lt 4 ]
do
echo '<div name="block-'${a}'">' >> $sub_main
((a++))
done
echo "done"
echo $sub_main
The output from the script should be:
<div name="block-0">
<div name="block-1">
<div name="block-2">
<div name="block-3">
>> appends the out putto a file. If you want to append to a variable you can use an assignment with the variable to append to at the beginning of the right value.
a=0;
sub_main="";
while [ "${a}" -lt 4 ]; do
sub_main="${sub_main}\n<div name=\"block-${a}\">";
((a++));
done;
echo "done";
echo "${sub_main}";
Or, if the appending to the file was intended, you can simply use cat instead of echo, to output the file's contents.
I.e. replace
echo $sub_main
with:
cat "${sub_main}";
Use command substitution to capture the output into a variable:
#!/bin/bash
sub_main=$(
for (( a = 0; a < 4; a++ )); do
printf '<div name="block-%d">\n' "$a"
done
)
# ... do other stuff ...
echo "$sub_main"
Or you might want to use a function to defer execution until later:
#!/bin/bash
sub_main() {
for (( a = 0; a < 4; a++ )); do
printf '<div name="block-%d">\n' "$a"
done
}
# ... do other stuff ...
sub_main

How to re-write powershell code in bash

I've been tasked with re-writing this in bash. But whilst most powershell is easy to read, i just dont get what this block is actually doing!!? Any ideas?
It takes a file which is sorted on a key first, maybe thats relevant!
Thanks for any insights!
foreach ($line in $sfile)
{
$row = $line.split('|');
if (-not $ops[$row[1]]) {
$ops[$row[1]] = 0;
}
if ($row[4] -eq '0') {
$ops[$row[1]]++;
}
if ($row[4] -eq '1') {
$ops[$row[1]]--;
}
#write-host $line $ops[$row[1]];
$prevrow = $row;
}
Perhaps a little refactoring would help:
foreach ($line in $sfile)
{
# $row is an array of fields on this line that were separated by '|'
$row = $line.split('|');
$key = $row[1]
$interestingCol = $row[4]
# Initialize $ops entry for key if it doesn't
# exist (or if key does exist and the value is 0, $null or $false)
if (-not $ops[$key]) {
$ops[$key] = 0;
}
if ($interestingCol -eq '0') {
$ops[$key]++;
}
elseif ($interestingCol -eq '1') {
$ops[$key]--;
}
#write-host $line $ops[$key];
# This appears to be dead code - unless it is used later
$prevrow = $row;
}
You are splitting a line on the '|' charater to an array row. It looks like you are using the $row array as some type of key into the $ops var. The first if test to see if the object exists if it doesn't it creates it in $ops the second and third ifs test to see if the 5th element in $row are zero and one and either increment or decrement the value created in the first if.
Approximately:
#!/bin/bash
saveIFS=$IFS
while read -r line
do
IFS='|'
row=($line)
# I don't know whether this is intended to test for existence or a boolean value
if [[ ! ${ops[${row[1]}] ]]
then
ops[${row[1]}]=0
fi
if (( ${row[4]} == 0 ))
then
(( ops[${row[1]}]++ ))
fi
if (( ${row[4]} == 1 ))
then
(( ops[${row[1]}]-- ))
fi
# commented out
# echo "$line ${ops[${row[1]}]}
prevrow=$row
done < "$sfile"

Resources