Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Wednesday, April 14, 2021

Print Raspberry Pi CPU and GPU temperature

cpu1=$(</sys/class/thermal/thermal_zone0/temp)
cpu2=$(echo "scale=1;$cpu1/1000" | bc)

gpu1=$(/opt/vc/bin/vcgencmd measure_temp)
gpu2=$(echo $gpu1 | tr -dc '.,0-9')

echo "$(date +'%d.%m.%Y %H:%M:%S') @ $(hostname)"

echo "CPU: $cpu2"
echo "GPU: $gpu2"

Monday, September 24, 2012

Bash behavior in Windows cmd.exe

Just found a perfect clink tool that extends Windows "cmd.exe" with Bash behaviors:
  • Powerful Bash-like line editing from GNU's Readline library. Read more on Readline's keyboard shortcuts.
  • Superior path completion (TAB).
  • Paste from clipboard (Ctrl-V).
  • Support for the completion of executables/commands, and environment variables.
  • Undo/Redo (Ctrl-_ or Ctrl-X, Ctrl-U)
  • Improved command line history.
    • Persists across sessions.
    • Searchable (Ctrl-R and Ctrl-S).
    • History expansion (e.g. !!, !, and !$).
  • Scriptable completion using Lua.
http://code.google.com/p/clink/

Thursday, March 10, 2011

How to make a file or folder invisible in Mac OS X Finder

Hide file or folder:

#!/bin/bash

setfile -a V filename

Make it visible again:

#!/bin/bash

setfile -a v filename

setfile is a part of Apple’s Developer Tools, that can be installed from Mac OS X installation DVD.

Monday, March 7, 2011

How to prompt and ask user input in Bash script

User the read command:

#!/bin/bash

echo This script requires running under account with administrator privileges
read -p "Enter name of administrator or root account: " adminUser

su - "$adminUser" -c "/bin/rm -f -r '/Applications/My Application.app'"

Friday, December 17, 2010

How to redirect application output in Bash script

Following code redirects standard output (STDOUT):

ls > /dev/null

Following code redirects error output (STDERR):

ls 2> /dev/null

Following code redirects both standard (STDOUT) and error output (STDERR):

ls &> /dev/null

How to kill a process by name in Bash script

Following code kills Safari application:

ps -axcopid,command | grep "Safari" | awk '{ system("kill -9 "$1) }'

Following pkill.sh script will kill all processes matching the given name (case-insensitive):

#!/bin/sh

if [[ $1 -eq "" ]]; then
    echo "Usage: pkill.sh <process name>"
    exit
fi

for id in `ps -axcopid,command | grep -i -e $1 | awk '{ print $1 }'`; do
  kill -9 $id
done

Thursday, December 16, 2010

How to show Yes/No prompt in Bash script

#!/bin/bash

read -p "Do you want to continue (Y/N)?"

[ "$(echo $REPLY | tr [:upper:] [:lower:])" == "y" ] || exit


or

#!/bin/bash

echo "Do you want to continue (yes/no)?"
read answer

if [ "$answer" == "yes" ]; then echo "YES"
else echo "NO"
fi


or

#!/bin/bash

while true; do
read -p "Do you want to continue (Y/N)?" answer
case $answer in
[Yy]* ) echo "YES"; break;;
[Nn]* ) echo "NO"; break;;
* ) echo "Please answer yes or no.";;
esac
done


or

#!/bin/bash

echo "What background color do you prefer?"

select answer in "White" "Blue" "Green"; do
case $answer in
White ) echo "WHITE"; break;;
Blue ) echo "BLUE"; break;;
Green ) echo "GREEN"; break;;
esac
done

How to convert strings to lower or upper case in Bash script

To lower case:

upper="YES"
lower="$(echo $upper | tr [:upper:] [:lower:])"
echo "$upper > $lower"

To upper case:

lower="no"
upper="$(echo $lower | tr [:lower:] [:upper:])"
echo "$lower > $upper"

Wednesday, December 15, 2010

How to enumerate files and directories in Bash script

Enumerate files:

for file in /Users/$USER/*; do
        if [ -f "$file" ]; then
            echo $file
        fi
done

Enumerate directories:

for file in /usr/*; do
        if [ -d "$file" ]; then
            echo $file
        fi
done

Tuesday, December 14, 2010

How to get current user name in Bash script

Using /usr/bin/whoami command:

#!/bin/bash

echo "$(whoami)"

Using /usr/bin/id command:

#!/bin/bash

echo "$(id -u -n)"

Using USER environment variable:

#!/bin/bash

echo "$USER"