Posts Tagged ‘bash’
sed regexp tips #1
Thanks to Jigen, this is another method to add and remove semicolon with sed. The result is the same as here.
To add semicolon:
sed -e ‘s/[01]/&;/g’ foo
To remove semicolon:
sed -e ‘s/\([01]\);/\1/g’ foo
a script to clone a directory but with empty files :P
This is my first seriuous bash script. Thanks in advance to people from #bash and #sed, thanks to Jigen, Dani and Arbiter.
I have a directory with some file inside. I want to create another directory with the same file (with same name) but empty (created with touch).
My code take two parameters:
copianomi.sh -i input_dir -o output_dir
Here my code:
#!/bin/bash
# ./copianomi.sh -i dirInput -o dirOutput
NO_ARGS=0
if [ $# -eq "$NO_ARGS" ] # Script invoked with no command-line args?
then
echo "Usage: $0 dirInput dirOutput"
exit # Exit and explain usage, if no argument(s) given.
fi
#
# check parameters
#
while getopts ":o:i:" Option
do
case $Option in
i ) INPUT=$OPTARG;;
o ) OUTPUT=$OPTARG;;
esac
done
#
# Copy names
#
for NAME in "$INPUT"/*; do
touch "$OUTPUT/${NAME##*/}"
done
Simple & Easy NAT between wifi and eth
Sometimes happen the same situation: one ethernet port, one ethernet cable, no hub/switch and many PCs with wireless.
A good solution is to connect a pc with ethernet and create a WLAN (ad hoc or infrastructure) to connect other PCs to internet.
Too many time I had this problem and the solution is always the same or similar, so I wrote this useful and reusable bash script to configure wifi card (chipset Atheros with madwifi driver) and create a NAT to connect AdHoc WLAN.
#!/bin/bash
# inizializing ethernet (I suppose outer network have address 193.205.22.12)
# I suggest to disable NetworkManager and kill dhclient
ifconfig eth0 193.205.22.12
# Now I need to unload and reload module with option "autocreate=adhoc". This
# simplify the creation of virttual interfaces athX (See how madwifi work for more informations)
/sbin/rmmod ath_pci
modprobe ath_pci autocreate=adhoc
# Configuring essid (In this case I use essid "spongepowa"
iwconfig ath0 essid spongepowa
# Configuring WLAN address (I suppose my network is 192.168.1.0/24)
ifconfig ath0 192.168.1.1
# Enabling forwarding
/bin/echo "1" > /proc/sys/net/ipv4/ip_forward
# Inizializing iptables
iptables --flush
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P PREROUTING ACCEPT
# Making rules:
# - 192.168.1.0/24 is local WLAN addresses
# - eth0 is the output interface (ethernet interface), ath0 is the input interface (wifi interface)
iptables -A FORWARD -s 192.168.1.0/24 -d 0/0 -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -d 192.168.1.0/24 -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.1.0/24 -j MASQUERADE
I hope this is correct. If you have any question ask me by comment.
If my english is full of mistakes, tell me (AYBABTU is not allowed without motivation :þ)…with my mistakes, of course.