Skip to content

Commit ad06152

Browse files
fix(shellcheck): multios-usb.sh (#54)
* fix(shellcheck): multios-usb.sh Signed-off-by: Sebastien Varrette <sebastien.varrette@ssi.gouv.fr> * Update config update prompts and version comparison * Hide unnecessary messages --------- Signed-off-by: Sebastien Varrette <sebastien.varrette@ssi.gouv.fr> Co-authored-by: MexIT <25229673+Mexit@users.noreply.github.com>
1 parent 9a51798 commit ad06152

1 file changed

Lines changed: 46 additions & 51 deletions

File tree

multios-usb.sh

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ trap '{
3535

3636
cd "$(dirname "$(readlink -f "$0")")"
3737

38-
echo "Arguments: $*" > $log_file
38+
echo "Arguments: $*" > "${log_file}"
3939

4040
showUsage() {
4141
cat <<- EOF
@@ -138,16 +138,16 @@ if [[ $updateOnly == yes ]]; then
138138
umount_partitions () {
139139
if [ "$manMounted" = true ]; then
140140
sudo umount "$part_data"
141-
rm -rf ${tmpdir}
141+
rm -rf "${tmpdir}"
142142
fi
143143
}
144144

145145
update_config () {
146-
echo -e "\n\e[1;41m++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\e[0m"
147-
echo -e "\e[1;41m++ Are you sure you want to update config files? ++\e[0m"
148-
echo -e "\e[1;41m++ All modified files in "config" directory will be removed! ++\e[0m"
149-
echo -e "\e[1;41m++ If you have modified any files, please copy them NOW to the "config_priv" directory. ++\e[0m"
150-
echo -e "\e[1;41m++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\e[0m"
146+
echo -e "\n\e[1;41m++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\e[0m"
147+
echo -e "\e[1;41m++ Are you sure you want to update config files? ++\e[0m"
148+
echo -e "\e[1;41m++ All modified files in \"config\" directory will be removed! ++\e[0m"
149+
echo -e "\e[1;41m++ If you have modified any files, please copy them NOW to the \"config_priv\" directory. ++\e[0m"
150+
echo -e "\e[1;41m++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\e[0m"
151151
echo -e "\nYour config files version: $currVer"
152152
echo -e "New config files version: $newVer"
153153
echo -en "\nType 'YeS' to continue: "
@@ -164,12 +164,12 @@ if [[ $updateOnly == yes ]]; then
164164
esac
165165

166166
echo "Updating..."
167-
rm -rf ${part_data}/MultiOS-USB/config
168-
cp -r config ${part_data}/MultiOS-USB
167+
rm -rf "${part_data}/MultiOS-USB/config"
168+
cp -r config "${part_data}/MultiOS-USB"
169169
}
170170

171171
echo -e "\nMultiOS-USB updater"
172-
part_data=$(findmnt -no TARGET ${devp}2) || true
172+
part_data=$(findmnt -no TARGET "${devp}2") || true
173173
if [ -z "$part_data" ]; then
174174
manMounted=true
175175
tmpdir=$(mktemp -d)
@@ -207,7 +207,8 @@ if [[ $updateOnly == yes ]]; then
207207
echo -e "\nConfig files version: $newVer"
208208
else
209209
OLDIFS=$IFS
210-
IFS=. v1=($newVer) v2=($currVer)
210+
IFS=. read -ra v1 <<< "$newVer"
211+
IFS=. read -ra v2 <<< "$currVer"
211212
IFS=$OLDIFS
212213

213214
for pos in 0 1 2; do
@@ -245,20 +246,14 @@ case "$fs_type" in
245246
esac
246247

247248
# Check for required software
248-
missing_soft="0"
249-
command -v dd &> /dev/null || { echo "dd is required but not installed."; missing_soft="1"; }
250-
command -v tar &> /dev/null || { echo "tar is required but not installed."; missing_soft="1"; }
251-
command -v xz &> /dev/null || { echo "xz is required but not installed."; missing_soft="1"; }
252-
command -v sgdisk &> /dev/null || { echo "sgdisk (gdisk) is required but not installed."; missing_soft="1"; }
253-
command -v wipefs &> /dev/null || { echo "wipefs is required but not installed."; missing_soft="1"; }
254-
command -v mkfs.fat &> /dev/null || { echo "mkfs.fat is required but not installed."; missing_soft="1"; }
255-
if [ "$fs_type" = "fat32" ]; then fs_prog="mkfs.fat"; else fs_prog="mkfs.$fs_type"; fi
256-
command -v $fs_prog &> /dev/null || { echo "$fs_prog is required but not installed."; missing_soft="1"; }
257-
258-
if [ "$missing_soft" -ne 0 ]; then
259-
echo -e "\n\e[0;41mNot all required programs are installed. Exiting...\e[0m\n"
249+
[ "$fs_type" = "fat32" ] && fs_prog="mkfs.fat" || fs_prog="mkfs.$fs_type"
250+
for cmd in dd tar xz sgdisk wipefs "$fs_prog"; do
251+
# shellcheck disable=SC2086
252+
if [ ! -x "$(command -v ${cmd} 2>/dev/null)" ]; then
253+
echo "${cmd} is required but not installed. Exiting"
260254
exit 1
261-
fi
255+
fi
256+
done
262257

263258
# Check for root
264259
if [ "$(id -u)" -ne 0 ]; then
@@ -286,28 +281,28 @@ esac
286281
umount -f "${devp}"* &> /dev/null || true
287282

288283
echo "Creating partitions..."
289-
sgdisk -Z "$dev" &>> $log_file
290-
sgdisk -n 1::"+${efi_size}" -t 1:0700 -c 1:"EFI System" -A 1:set:0 -A 1:set:62 -A 1:set:63 "$dev" &>> $log_file
291-
sgdisk -n 2::"${data_size}" -t 2:"$part_code" -c 2:"$part_name" "$dev" &>> $log_file
284+
sgdisk -Z "$dev" &>> "$log_file"
285+
sgdisk -n 1::"+${efi_size}" -t 1:0700 -c 1:"EFI System" -A 1:set:0 -A 1:set:62 -A 1:set:63 "$dev" &>> "$log_file"
286+
sgdisk -n 2::"${data_size}" -t 2:"$part_code" -c 2:"$part_name" "$dev" &>> "$log_file"
292287

293-
wipefs -af "${devp}1" &>> $log_file
294-
wipefs -af "${devp}2" &>> $log_file
288+
wipefs -af "${devp}1" &>> "$log_file"
289+
wipefs -af "${devp}2" &>> "$log_file"
295290

296291
echo "Formating partitions..."
297-
mkfs.fat -F 16 -n "MultiOS-EFI" "${devp}1" &>> $log_file
292+
mkfs.fat -F 16 -n "MultiOS-EFI" "${devp}1" &>> "$log_file"
298293

299294
case "$fs_type" in
300295
ext2|ext3|ext4)
301-
mkfs.${fs_type} -L "$data_label" "${devp}2" &>> $log_file
296+
mkfs."${fs_type}" -L "$data_label" "${devp}2" &>> "$log_file"
302297
;;
303298
fat32)
304-
mkfs.fat -F 32 -n "$data_label" "${devp}2" &>> $log_file
299+
mkfs.fat -F 32 -n "$data_label" "${devp}2" &>> "$log_file"
305300
;;
306301
exfat)
307-
mkfs.exfat -n "$data_label" "${devp}2" &>> $log_file
302+
mkfs.exfat -n "$data_label" "${devp}2" &>> "$log_file"
308303
;;
309304
ntfs)
310-
mkfs.ntfs --fast -L "$data_label" "${devp}2" &>> $log_file
305+
mkfs.ntfs --fast -L "$data_label" "${devp}2" &>> "$log_file"
311306
;;
312307
*)
313308
echo "Error! $fs_type is an invalid filesystem type."
@@ -320,36 +315,36 @@ part_data="${tmpdir}/part_data"
320315
part_efi="${tmpdir}/part_efi"
321316
mkdir "$part_data" "$part_efi"
322317

323-
mount ${devp}1 $part_efi
324-
mount ${devp}2 $part_data
318+
mount "${devp}1" "$part_efi"
319+
mount "${devp}2" "$part_data"
325320

326321
echo "Copying files..."
327-
mkdir -p $part_data/{MultiOS-USB/tools,ISOs} $part_efi/{EFI/BOOT,grub/fonts}
328-
cp -r config config_priv themes LICENSE README.md MultiOS-USB.version $part_data/MultiOS-USB
329-
cp -r binaries/{syslinux-*,mt86plus_*,efitools-*,wimboot-*,mountiso} $part_data/MultiOS-USB/tools
322+
mkdir -p "$part_data"/{MultiOS-USB/tools,ISOs} "$part_efi"/{EFI/BOOT,grub/fonts}
323+
cp -r config config_priv themes LICENSE README.md MultiOS-USB.version "$part_data/MultiOS-USB"
324+
cp -r binaries/{syslinux-*,mt86plus_*,efitools-*,wimboot-*,mountiso} "$part_data/MultiOS-USB/tools"
330325

331326
echo "Installing bootloader..."
332-
tar -xf binaries/grub-*/i386-pc.tar.xz -C $part_efi/grub
327+
tar -xf binaries/grub-*/i386-pc.tar.xz -C "$part_efi/grub"
333328

334-
cat > $part_efi/grub/grub.cfg << EOF
329+
cat > "$part_efi/grub/grub.cfg" << EOF
335330
search -f /MultiOS-USB/config/grub.config --no-floppy --set=root
336331
source /MultiOS-USB/config/grub.config
337332
EOF
338333

339-
cp binaries/grub-*/grubenv $part_efi/grub
340-
cp -r binaries/grub-*/unicode.pf2 $part_efi/grub/fonts
341-
cp -r binaries/shim-signed_*/*.efi $part_efi/EFI/BOOT
342-
cp binaries/grub-*/grubx64.efi $part_efi/EFI/BOOT
343-
cp -r cert/ $part_efi/EFI/
334+
cp binaries/grub-*/grubenv "$part_efi/grub"
335+
cp -r binaries/grub-*/unicode.pf2 "$part_efi/grub/fonts"
336+
cp -r binaries/shim-signed_*/*.efi "$part_efi/EFI/BOOT"
337+
cp binaries/grub-*/grubx64.efi "$part_efi/EFI/BOOT"
338+
cp -r cert/ "$part_efi/EFI/"
344339

345340
dd conv=fsync status=none if="$part_efi/grub/i386-pc/boot.img" of="${dev}" bs=1 count=446
346341
dd conv=fsync status=none if="$part_efi/grub/i386-pc/core.img" of="${dev}" bs=512 count=2014 seek=34
347342

348-
mv "$log_file" $part_data/MultiOS-USB/install.log
349-
chmod -R o+rw $part_data
343+
mv "$log_file" "$part_data/MultiOS-USB/install.log"
344+
chmod -R o+rw "$part_data"
350345

351346
sync
352-
umount $part_efi
353-
umount $part_data
354-
rm -rf ${tmpdir}
347+
umount "$part_efi"
348+
umount "$part_data"
349+
rm -rf "${tmpdir}"
355350
echo -e "\n\e[0;42mMultiOS-USB has been successfully installed.\e[0m\n"

0 commit comments

Comments
 (0)