Bash
Jump to navigation
Jump to search
Documentation
- Advanced Bash-Scripting Guide
- http://www.ss64.com/bash/index.html
- http://tille.xalasys.com/training/bash
Shell
Listing directory content:
ls --full-time # To list files full date & time
ls -d directory-name # List directory entries, not content
# eg. ls -d /etc/rc*
ls -lS | head # List biggest files first - print only first 10 entries
ls -lt | head -n 20 # List most recent files first, print only 20 first entries
Script Quick Tutorial
Environment
export MYVAR=myvalue && command-to-execute #MYVAR defined for the current shell and invoked shell
MYVAR=myvalue && command-to-execute #MYVAR defined for the current shell
MYVAR=myvalue command-to-execute #MYVAR only defined for the subsequent command
Loops
# On several lines
for f in $(seq 33296 33330); do
wget "http://i.techrepublic.com.com/gallery/${f}.jpg"
done
# On a single line
for i in `seq 278 328`; do wget http://i.techrepublic.com.com/gallery/33$i.jpg; done
Interactivity
Read User's password
Reading input from user is done with the commands read. To prevent password echo on display, one can use the command stty:
###### Preventing echo with stty ######
read -p "Username: " uname
stty -echo
read -p "Password: " passw; echo
stty echo
Preventing password echo using option -s:
###### Using -s ######
PASS="abc123"
read -s -p "Password: " mypassword
echo ""
[ "$mypassword" == "$PASS" ] && echo "Password accepted" || echo "Access denied"
Complete solution:
###### Complete solution ######
USER=wbi\\titeuf
echo "Mounting windows share... Please type password for user $USER..."
# time-out after 60sec, raw input no escaping, no echo, prompt
read -t 60 -r -s -p "Password: " PASSWORD
# delete prompt line
echo -e -n "\r"
mount -t smbfs -o username="$USER",password="$PASSWORD",iocharset=iso8859-1,codepage=cp437 //windows-host/C$ /mnt/c
mount -t smbfs -o username="$USER",password="$PASSWORD",iocharset=iso8859-1,codepage=cp437 //windows-host/D$ /mnt/d
mount -t smbfs -o username="$USER",password="$PASSWORD",iocharset=iso8859-1,codepage=cp437 //windows-host/F$ /mnt/f
# delete password variable
PASSWORD=------------------------------------
PASSWORD=abcdefghijklmnopqrstuvwxyz0123456789
PASSWORD=------------------------------------
Miscellaneous
Execute the output of a process
eval 'dircolors'