Bash script that goes trough folder and uses dd

43 views Asked by At

Hi I am looking to create a script in bash/sh that can do the following

  1. Go in to a folder
  2. Cycles trough a set of img.xz files and if it finds one of the secified ones uses that to invoke unxz -c (img.xzfile) | dd of=/dev/mmcblk0 bs=4096 status=progress

The folder will at least contain 3-5 img.xz

What I have now

#!bin/bash 
$img.xz
$img.xz2 

cd /img/ 

if ./20240317-1928-postmarketOS-edge-i3wm-0.5-microsoft-surface-rt.img.xz -->$img.xz
then unxz -c $img.xz | dd of=/dev/mmcblk0 bs=4096 status=progress

else 
./20240317-1346-postmarketOS-edge-mate-7-microsoft-surface-rt.img.xz --> $img.xz2
then unxz -c $img.xz2 | dd of=/dev/mmcblk0 bs=4096 status=progress

I don't know if this works, hence the question for something like this that can work, or even lists all images in the folder and asks witch one to use by selecting option 1-5 put that in the variable and use the variable in the unxz -c $variable | dd of=/dev/mmcblk0 bs=4096 status=progress to image the to a emmc module build in a device.

This script will be run from a USB booted live Linux image with the images in the /img/ folder

1

There are 1 answers

4
Yogesh Jha On

You can achieve your requirement by iterating through the .img.xz files in the folder and providing options to the user to select which image to use. Here's a script that does just that:

#!/bin/bash

# Change directory to the folder containing the .img.xz files
cd /img/ || exit

# Array to store available image files
img_files=()

# Find .img.xz files in the current directory
while IFS=  read -r -d $'\0'; do
    img_files+=("$REPLY")
done < <(find . -maxdepth 1 -type f -name "*.img.xz" -print0)

# Check if any image files were found
if [ ${#img_files[@]} -eq 0 ]; then
    echo "No .img.xz files found in the /img/ folder."
    exit 1
fi

echo "Available image files:"
for ((i=0; i<${#img_files[@]}; i++)); do
    echo "$(($i+1)). ${img_files[$i]}"
done

# Prompt user to select an image file
read -rp "Enter the number corresponding to the image file you want to use: " selection

# Validate user input
if ! [[ "$selection" =~ ^[1-9]+$ ]] || ((selection < 1)) || ((selection > ${#img_files[@]})); then
    echo "Invalid selection. Please enter a number between 1 and ${#img_files[@]}."
    exit 1
fi

# Get the selected image file
selected_img="${img_files[$((selection - 1))]}"

# Extract and write the image to the /dev/mmcblk0 device
unxz -c "$selected_img" | dd of=/dev/mmcblk0 bs=4096 status=progress

echo "Image flashing complete."

This script:

  • Changes directory to the /img/ folder.
  • Searches for .img.xz files in the current directory.
  • Displays a numbered list of available image files.
  • Prompts the user to select an image file by entering the corresponding number.
  • Validates the user input to ensure it's a valid selection.
  • Extracts and writes the selected image file to the /dev/mmcblk0 device using unxz and dd.
  • Prints a message when the image flashing is complete.

Make sure to run this script with appropriate permissions and be cautious when writing to the /dev/mmcblk0 device, as it can potentially overwrite data.