Skip to content

Instantly share code, notes, and snippets.

@BoyanXu
Last active May 24, 2023 11:07
Show Gist options
  • Save BoyanXu/a3a7845dd1a9e60f7f1847e57f2e861b to your computer and use it in GitHub Desktop.
Save BoyanXu/a3a7845dd1a9e60f7f1847e57f2e861b to your computer and use it in GitHub Desktop.
Check how many binary files installed through /usr/local/bin/brew are x86-based
#!/usr/local/bin/fish
rm list-of-* 2> /dev/null
## Get list-of-bin, list-of-pkg
exa -al /usr/local/bin | grep Cellar | awk '{ print "/usr/local/bin/" $7}' | sort | uniq >> list-of-bin
exa -al /usr/local/bin | grep Cellar | awk '{ print $9}' | sort | uniq | awk -F'/Cellar/' '{split($2,a,"/"); print a[1]}' | sort | uniq >> list-of-pkg
## Get list-of-arch
while read -la file
echo -en "$file \t" | tee -a list-of-arch
file -b "$file" | tee -a list-of-arch
end < list-of-bin
## Get list-of-x86-bin
cat list-of-arch | grep "Mach-O 64-bit executable x86_64" | grep -v "Mach-O universal binary" | awk '{ print $1 }' > list-of-x86-bin
## Get list-of-x86-arch
while read -la file
echo -en "$file \t" | tee -a list-of-x86-arch
file -b "$file" | tee -a list-of-x86-arch
end < list-of-x86-bin
## Get list-of-x86-pkg
while read -la file
exa -al "$file" | awk '{ print $9}' | awk -F'/Cellar/' '{split($2,a,"/"); print a[1]}' | tee -a list-of-x86-pkg
end < list-of-x86-bin
cat list-of-x86-pkg | sort | uniq> list-of-x86-pkg.tmp && mv list-of-x86-pkg.tmp list-of-x86-pkg
## Get arm64 support availability for the currently installed x86-pkgs
while read -la file
echo -en "$file \t" | tee -a list-of-m1-support
brew info --json fish | jq -r '.[0].bottle.stable.files | keys[] | select(contains("arm64")) | @sh' | paste -sd "," - | sed 's/\"//g' | tee -a list-of-m1-support
end < list-of-x86-pkg
@JesusCastroFernandez
Copy link

El código proporcionado parece ser un script escrito en el shell de Fish. A continuación, se presentan algunas mejoras y comentarios para hacerlo más legible, eficiente y fácil de mantener:

Añadir comentarios: Agrega comentarios explicativos para ayudar a comprender el propósito y la funcionalidad de cada sección del código.
`#!/usr/local/bin/fish

Limpiar archivos existentes

rm list-of-* 2> /dev/null

Obtener lista de binarios y paquetes

exa -al /usr/local/bin | grep Cellar | awk '{ print "/usr/local/bin/" $7}' | sort | uniq >> list-of-bin
exa -al /usr/local/bin | grep Cellar | awk '{ print $9}' | sort | uniq | awk -F'/Cellar/' '{split($2,a,"/"); print a[1]}' | sort | uniq >> list-of-pkg

Obtener lista de arquitecturas

while read -la file
echo -en "$file \t" | tee -a list-of-arch
file -b "$file" | tee -a list-of-arch
end < list-of-bin

Obtener lista de binarios x86

cat list-of-arch | grep "Mach-O 64-bit executable x86_64" | grep -v "Mach-O universal binary" | awk '{ print $1 }' > list-of-x86-bin

Obtener lista de arquitecturas x86

while read -la file
echo -en "$file \t" | tee -a list-of-x86-arch
file -b "$file" | tee -a list-of-x86-arch
end < list-of-x86-bin

Obtener lista de paquetes x86

while read -la file
exa -al "$file" | awk '{ print $9}' | awk -F'/Cellar/' '{split($2,a,"/"); print a[1]}' | tee -a list-of-x86-pkg
end < list-of-x86-bin
cat list-of-x86-pkg | sort | uniq > list-of-x86-pkg.tmp && mv list-of-x86-pkg.tmp list-of-x86-pkg

Obtener disponibilidad de soporte arm64 para los paquetes x86 instalados actualmente

while read -la file
echo -en "$file \t" | tee -a list-of-m1-support
brew info --json fish | jq -r '.[0].bottle.stable.files | keys[] | select(contains("arm64")) | @sh' | paste -sd "," - | sed 's/"//g' | tee -a list-of-m1-support
end < list-of-x86-pkg
Uso de variables: Almacenar rutas de archivos o comandos comunes en variables para facilitar la modificación y comprensión del código.#!/usr/local/bin/fish

Variables

set binListFile "list-of-bin"
set pkgListFile "list-of-pkg"
set archListFile "list-of-arch"
set x86BinListFile "list-of-x86-bin"
set x86ArchListFile "list-of-x86-arch"
set x86PkgListFile "list-of-x86-pkg"
set m1SupportListFile "list-of-m1-support"

Limpiar archivos existentes

rm $binListFile $pkgListFile 2> /dev/null

Obtener lista de binarios y paquetes

exa -al /usr/local/bin | grep Cellar | awk '{ print "/usr/local/bin/" $7}' | sort | uniq >> $binListFile
exa -al /usr/local/bin | grep Cellar | awk '{ print $9}' | sort | uniq | awk -F'/Cellar/' '{split($2,a,"/"); print a[1]}' | sort | uniq >> $pkgListFile

Obtener lista de arquitecturas

while read -la file
echo -en "$file \t" | tee -a $archListFile
file -b "$file" | tee -a $archListFile
end < $binListFile

Obtener lista de binarios x86

cat $archListFile | grep "Mach-O 64-bit executable x86_64" | grep -v "Mach-O universal binary" | awk '{ print $1 }' > $x86BinListFile

Obtener lista de arquitecturas x86

while read -la file
echo -en "$file \t" | tee -a $x86ArchListFile
file -b "$file" | tee -a $x86ArchListFile
end < $x86BinListFile

Obtener lista de paquetes x86

while read -la file
exa -al "$file" | awk '{ print $9}' | awk -F'/Cellar/' '{split($2,a,"/"); print a[1]}' | tee -a $x86PkgListFile
end < $x86BinListFile
cat $x86PkgListFile | sort | uniq > $x86PkgListFile.tmp && mv $x86PkgListFile.tmp $x86PkgListFile

Obtener disponibilidad de soporte arm64 para los paquetes x86 instalados actualmente

while read -la file
echo -en "$file \t" | tee -a $m1SupportListFile
brew info --json fish | jq -r '.[0].bottle.stable.files | keys[] | select(contains("arm64")) | @sh' | paste -sd "," - | sed 's/"//g' | tee -a $m1SupportListFile
end < $x86PkgListFile
Uso de shebang más general: El shebang #!/usr/local/bin/fish limita el script a ejecutarse solo en sistemas donde Fish se encuentra en esa ruta específica. Siempre es mejor usar una ruta más general, como #!/usr/bin/env fish, para que el intérprete de Fish se encuentre en el entorno.#!/usr/bin/env fish
`
Estas sugerencias deberían mejorar la legibilidad y mantenibilidad del código. Sin embargo, ten en cuenta que no se puede garantizar su funcionalidad o rendimiento sin un contexto adicional sobre el objetivo del script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment