View previous topic :: View next topic |
Author |
Message |
Termina God
Joined: 06 Mar 2004 Posts: 722 Location: Sheol
|
Posted: Mon Dec 20, 2004 9:55 pm Post subject: [ Tutorial ] BASH/BAT Script Examples |
|
|
Post any good BASH scripts you made/have!
Code: |
#!/bin/bash
# Made by: Termina
# Fairly self explanatory:
# It gets each folder name from /home
# and makes sure that the folder is owned by
# by who it should be owned by.
# It also double-checks the permissions
# in case a user made a mistake and made
# something writeable/viewable/executable to group.
# This script makes sure that other users cannot
# view other users files, but still allows
# apache (group:nobody) to view/exec files.
# Make sure to useradd -m -G users username!
# Also makes sure that /home is executable.
# Set up in crontab to run once an hour
for x in `ls /home`
do
chown -R $x /home/$x
chgrp -R users /home
chmod -R 705 /home
chmod 715 /home
done
|
Code: |
#!/bin/bash
# Made by: Termina
# Make sure to set this up as crontab to run
# once a day. No more, no less.
# this makes /root/history/username/DayMonthDateYear
# and copies .bash_history
# This is useful if the user periodicly clears their bash_history
# You could also back up other important files for the users.
# Example: cp -r /home/$x/public_html /root/history/$x/`date +%a%b%d%y`
for x in `ls /home`
do
mkdir /root/history
mkdir /root/history/$x
mkdir /root/history/$x/`date +%a%b%d%y`
cp /home/$x/.bash_history /root/history/$x/`date +%a%b%d%y`
|
Code: |
#!/bin/bash
# Concept by Hemant, Script made by Termina
# This script not only backs up a directory/files
# but checks to see if the filesize has changed
# with du -b (checks bytes, instead of apparent size)
# It will also remove files that are no longer in
# APREFIX (ie, don't need to backup).
# It only seems to work if you put it in the APREFIX dir.
# Not a big deal, since permissions on it should be fine.
# I really like this script, and am still touching it up.
APREFIX="/home/andrew"
BPREFIX="/usr/backup"
SIZE="du -b" #the command to check the size
cd $APREFIX
for x in `ls -R`
do
sizeA = du -b "$x"
sizeB = du -b "$BPREFIX/$x"
if [ "sizeA" = "sizeB" ]
then
echo "File sizes the same"
else
cp -r $x $BPREFIX
echo $x
fi
cd $BRREFIX
for x in `ls -R`
do
if [ "$APREFIX"/"$x" ]
then
echo $x
#do nothing
else
rm "$x" "$x" because the file is no longer in "A"
fi
done
done
|
Hemant put up a concept of it, which didn't work and was horribly bugged. Decided to fix it up, debug, etc.
Giving him credit, since it would be in bad taste to steal it.
Code: |
#!/bin/bash
# I found that tar wasn't working well
# (or at all) on my server. Something about
# not being able to execute 'compress'.
# Anyways, I made a script that uses zip.
# Still trying to find out how to use mail to send
# .zip and other binary files
for x in `ls /home`
do
mkdir /root/history
mkdir /root/history/$x
zip -r /root/history/$x/`date +%a%b%d%y` /home/$x
done
|
Last edited by Termina on Sat Jan 29, 2005 6:38 am; edited 5 times in total |
|
Back to top |
|
|
andrew Minion
Joined: 14 Mar 2004 Posts: 233 Location: from an alternate dimension from our own, of a space and time beyond what mere human men call life
|
Posted: Mon Dec 20, 2004 10:40 pm Post subject: Re: BASH Scripts |
|
|
Termina wrote: |
Post any good BASH scripts you made/have!
Code: |
... ... ...
for x in `ls /home`
do
chown -R $x /home/$x
chgrp -R users /home
chmod -R 705 /home
chmod 715 /home
done
|
|
Note: (from personal experience using termina's lame ass instructions ) don't use those permission schemes if you are running X. X will *need* to read from some files in your home dir... _________________ If the minimum wasn't acceptable, it wouldn't be called the minimum. |
|
Back to top |
|
|
Termina God
Joined: 06 Mar 2004 Posts: 722 Location: Sheol
|
Posted: Mon Dec 20, 2004 10:41 pm Post subject: |
|
|
This is mostly for servers. :P
You could probally fix the X issues by making a special set of rules
for the X-needed files in your home dir. Not sure what they are.
.xsession, .ICEAuthority, etc.
Instead of adding more posts, I'm going to edit my first one and add scripts to it, or edit them. |
|
Back to top |
|
|
andrew Minion
Joined: 14 Mar 2004 Posts: 233 Location: from an alternate dimension from our own, of a space and time beyond what mere human men call life
|
Posted: Mon Dec 20, 2004 11:10 pm Post subject: |
|
|
Code: |
#! /bin/bash
# If anyone runs Blackbox and hates setting the wallpaper everytime
# they log onto X, here's a script that should do it for you, and leave it in
# your .bash_profile. So it will load this wallpaper everytime you load
# X. :D -Just make sure you set your path to your .bash_profile... :P
#
# Wallpaper 1.0
#
echo -n "Name of wallpaper: "
read -e WALLPAPER
display -window root $WALLPAPER
echo "display -window root $WALLPAPER" >> /home/username/.bash_profile
echo "Wallpaper set, bitch!"
|
Code: |
#!/bin/bash
# A script to back up your home dir periodically.
# You will have to execute this script in your
# crontab, But from what I can see, this looks
# like it should work properly
#
# Home Dir backup 1.0
#
SRCD="/home/andrew/"
TGTD="/home/andrew/backups/"
OF=home-$(date +%Y%m%d).tgz
tar -cZf $SRCD $TGTD$OF
|
_________________ If the minimum wasn't acceptable, it wouldn't be called the minimum. |
|
Back to top |
|
|
andrew Minion
Joined: 14 Mar 2004 Posts: 233 Location: from an alternate dimension from our own, of a space and time beyond what mere human men call life
|
Posted: Thu Dec 30, 2004 8:58 pm Post subject: |
|
|
Code: |
#!/bin/bash
#
# Linux Shell Scripting Tutorial 1.05r3, Summer-2002
#
# Written by Vivek G. Gite <vivek@nixcraft.com>
#
# Latest version can be found at http://www.nixcraft.com/
#
#
nouser=`who | wc -l`
echo -e "User name: $USER (Login name: $LOGNAME)" >> /tmp/info.tmp.01.$$$
echo -e "Current Shell: $SHELL" >> /tmp/info.tmp.01.$$$
echo -e "Home Directory: $HOME" >> /tmp/info.tmp.01.$$$
echo -e "Your O/s Type: $OSTYPE" >> /tmp/info.tmp.01.$$$
echo -e "PATH: $PATH" >> /tmp/info.tmp.01.$$$
echo -e "Current directory: `pwd`" >> /tmp/info.tmp.01.$$$
echo -e "Currently Logged: $nouser user(s)" >> /tmp/info.tmp.01.$$$
if [ -f /etc/slackware-version ]
then
echo -e "OS: `cat /etc/slackware-version`" >> /tmp/info.tmp.01.$$$
fi
if [ -f /etc/shells ]
then
echo -e "Available Shells: " >> /tmp/info.tmp.01.$$$
echo -e "`cat /etc/shells`" >> /tmp/info.tmp.01.$$$
fi
if [ -f /etc/sysconfig/mouse ]
then
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Computer Mouse Information: " >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "`cat /etc/sysconfig/mouse`" >> /tmp/info.tmp.01.$$$
fi
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Computer CPU Information:" >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
cat /proc/cpuinfo >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Computer Memory Information:" >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
cat /proc/meminfo >> /tmp/info.tmp.01.$$$
if [ -d /proc/ide/hda ]
then
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Hard disk information:" >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "Model: `cat /proc/ide/hda/model` " >> /tmp/info.tmp.01.$$$
echo -e "Driver: `cat /proc/ide/hda/driver` " >> /tmp/info.tmp.01.$$$
echo -e "Cache size: `cat /proc/ide/hda/cache` " >> /tmp/info.tmp.01.$$$
fi
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
echo -e "File System (Mount):" >> /tmp/info.tmp.01.$$$
echo -e "--------------------------------------------------------------------" >> /tmp/info.tmp.01.$$$
cat /proc/mounts >> /tmp/info.tmp.01.$$$
if which dialog > /dev/null
then
dialog --backtitle "Linux Software Diagnostics
(LSD) Shell Script Ver.1.0" --title "Press Up/Down Keys to move"
--textbox /tmp/info.tmp.01.$$$ 21 70
else
cat /tmp/info.tmp.01.$$$ |more
fi
rm -f /tmp/info.tmp.01.$$$
#
#
|
just a neat little script to display a lot of your info.
--** for the lazy and uninformed! **-- _________________ If the minimum wasn't acceptable, it wouldn't be called the minimum. |
|
Back to top |
|
|
Termina God
Joined: 06 Mar 2004 Posts: 722 Location: Sheol
|
Posted: Fri Dec 31, 2004 3:43 am Post subject: |
|
|
Nice find, andrew. Here's a script for MS-DOS that some people might find useful to have set up in the schedular.
Code: |
:::::::::::::::::::::::::::::::::::::::
::::::: Created by Will Twomey ::::::
::::::: 3:43 PM 6/25/2004 ::::::::::
:::::::::::::::::::::::::::::::::::::::
ECHO.
@ECHO OFF
CLS
ECHO.
ECHO ESTABLISHING NETWORK CONNECTION.......PLEASE WAIT
rem if "%1()"=="()" goto default
rem choice /c:y /t:y,3 /n
rem goto end
rem :default
rem choice /c:y /t:y,3 /n
rem :end
ECHO NETWORK CONNECTION ESTABLISHED........PLEASE WAIT
ECHO.
rem if "%1()"=="()" goto default1
rem choice /c:y /t:y,1 /n
rem goto end1
rem :default1
rem choice /c:y /t:y,4 /n
:end1
ECHO.
ECHO.
CLS
ECHO.
ECHO CLEANING CURRENT DIRECTORIES...
ECHO.
rem choice /c:y /t:y,4 /n
DEL /f /s /q C:\BACKUP\CORECOM01\MY_DOCS\*.* >nul
DEL /f /s /q C:\BACKUP\CORECOM01\APPROACH\*.* >nul
REM
DEL /f /s /q C:\Backup\CORECOM02\WORK\*.* >nul
DEL /f /s /q C:\BACKUP\CORECOM02\MY_DOCS\*.* >nul
REM
DEL /f /s /q C:\Backup\CORECOM03\WORK\*.* >nul
DEL /f /s /q C:\BACKUP\CORECOM03\Manager\*.* >nul
DEL /f /s /q C:\BACKUP\CORECOM03\MY_DOCS\*.* >nul
REM
DEL /f /s /q C:\Backup\CORECOM4\WORK\*.* >nul
DEL /f /s /q C:\BACKUP\CORECOM4\UPS\*.* >nul
DEL /f /s /q C:\BACKUP\CORECOM4\MY_DOCS\*.* >nul
echo.
cls
ECHO.
ECHO COPYING FILES.....PLEASE WAIT
ECHO.
ECHO ********************************************************
rem choice /c:y /t:y,4 /n
ECHO.
ECHO Copying APPROACH from Corecom01...please wait
echo.
XCOPY \\Corecom01\C\lotus\approach /S C:\Backup\CORECOM01\APPROACH
ECHO.
ECHO WORK successfully transfered
ECHO.
rem choice /c:y /t:y,4 /n
CLS
ECHO.
ECHO Copying My Documents from Corecom01...please wait
ECHO.
XCOPY \\CORECOM01\My Documents\ /S C:\BACKUP\CORECOM01\MY_DOCS
ECHO.
ECHO My Documents successfully transfered
ECHO.
choice /c:y /t:y,4 /n
REM **************************************************************
rem choice /c:y /t:y,4 /n
ECHO.
ECHO Copying WORK from Corecom02...please wait
echo.
XCOPY \\CORECOM05\WORK\ /S C:\Backup\CORECOM02\WORK
ECHO.
ECHO WORK successfully transfered
ECHO.
rem choice /c:y /t:y,4 /n
CLS
ECHO.
ECHO Copying My Documents from Corecom02...please wait
ECHO.
XCOPY \\CORECOM05\my_doc\ /S C:\BACKUP\CORECOM02\MY_DOCS
ECHO.
ECHO My Documents successfully transfered
ECHO.
choice /c:y /t:y,4 /n
REM **************************************************************
CLS
ECHO.
ECHO Copying Manager directory from Corecom03...please wait
ECHO.
XCOPY \\CORECOM03\MANAGER\ /s C:\BACKUP\CORECOM03\Manager
ECHO.
ECHO Manager successfully transfered
ECHO.
rem choice /c:y /t:y,4 /n
ECHO.
ECHO Copying WORK from Corecom03...please wait
ECHO.
XCOPY \\CORECOM03\WORK\ /S C:\BACKUP\CORECOM03\WORK
ECHO.
ECHO WORK successfully transfered
ECHO.
rem choice /c:y /t:y,4 /n
CLS
ECHO.
ECHO Copying My Documents from Corecom03...please wait
ECHO.
XCOPY \\CORECOM03\MY_DOCS /S C:\BACKUP\CORECOM03\MY_DOCS
ECHO.
ECHO My Documents successfully transfered
Echo.
rem choice /c:y /t:y,4 /n
CLS
ECHO ********************************************************
ECHO.
ECHO BATCH PROCESS COMPLETED SUCCESSFULLY
ECHO.
rem choice /c:y /t:y,3 /n
CLS
ECHO.BATCH PROCESS COMPLETED SUCCESSFULLY >TIME.LOG
ECHO.
ECHO. |TIME > TIME.LOG
ECHO.
ECHO. |DATE >> TIME.LOG
ECHO.
rem START /M C:\PROGRA~1\Adaptec\Easycd~1\createcd\createcd.exe
"C:\Program Files\Roxio\Easy CD Creator 6\Easy CD Creator\creatorc.exe"
rem del /y C:\BACKUP\batch.pif
rem choice /c:y /t:y,4 /n
@cls
EXIT
|
I made this script for the company downstairs. Works well, and it
opens up their CD-Burning software when they're done as well. Nifty
stuff. Stole concept from the guy who originally set up their network,
but have pretty much rewritten it over the course of several months. |
|
Back to top |
|
|
andrew Minion
Joined: 14 Mar 2004 Posts: 233 Location: from an alternate dimension from our own, of a space and time beyond what mere human men call life
|
Posted: Wed Jan 05, 2005 7:40 am Post subject: |
|
|
Code: |
#!/bin/bash
# htmlpage
# Make by Andrew! Obviously.
#
# This will just cat those normal tags that are incredibly
# annoying to make, but is awfully handy and convenient!
cat << _eof_
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>
_eof_ |
_________________ If the minimum wasn't acceptable, it wouldn't be called the minimum. |
|
Back to top |
|
|
andrew Minion
Joined: 14 Mar 2004 Posts: 233 Location: from an alternate dimension from our own, of a space and time beyond what mere human men call life
|
Posted: Sat Jan 15, 2005 1:15 am Post subject: |
|
|
Code: |
#!/bin/bash
#
# I got incredibly tired of not being able to copy and paste out of xterm,
# so i made this little thing to stick a command's output in my webserv.
# Not too brilliant, but it is a fix for an annoyance. Just give the link to
# show the output to others! :D
# Made by Andrew
#
echo -n "Command: "
read -e COMMAND
exec $COMMAND > /path/to/webserv/docroot/filename
|
_________________ If the minimum wasn't acceptable, it wouldn't be called the minimum. |
|
Back to top |
|
|
andrew Minion
Joined: 14 Mar 2004 Posts: 233 Location: from an alternate dimension from our own, of a space and time beyond what mere human men call life
|
Posted: Wed Feb 09, 2005 7:19 pm Post subject: |
|
|
I'm needing a little help with this script:
Code: |
#!/bin/bash
echo -n "Directory containing the files you wish to change: "
read -e DIRECTORY
cd $DIRECTORY
for I in *
do
newname=$(echo $I | sed 's/ /_/g')
oldname=$(echo $I)
mv "$oldname" "$newname"
done
for B in *
do
goodchars=$(echo "$B" | tr -d '')
badchars=$(echo $B)
mv "$badchars" "$goodchars"
done
|
The script is meant to change spaces to underscores and completely
truncate all non-number/letter characters. I'm assuming I will need to
either list all the characters I need to delete, or rig the '[:alnum:]'
option for 'tr' in there somewhere.
Everything is working right, except for the part where I need to
just truncate all the non [:alnum:] chars. So I just left that part
blank:
Code: |
goodchars=$(echo "$B" | tr -d '')
|
Any help? I'm getting frustrated. _________________ If the minimum wasn't acceptable, it wouldn't be called the minimum. |
|
Back to top |
|
|
Kalzz DemiGod
Joined: 09 Mar 2004 Posts: 597 Location: Who wants to know <.< >.>
|
Posted: Wed Feb 09, 2005 10:05 pm Post subject: |
|
|
Can you tokenize a string? _________________ -Kalzz
|
|
Back to top |
|
|
andrew Minion
Joined: 14 Mar 2004 Posts: 233 Location: from an alternate dimension from our own, of a space and time beyond what mere human men call life
|
Posted: Wed Feb 09, 2005 10:12 pm Post subject: |
|
|
I'm not sure about strings... I highly doubt it though. _________________ If the minimum wasn't acceptable, it wouldn't be called the minimum. |
|
Back to top |
|
|
Kalzz DemiGod
Joined: 09 Mar 2004 Posts: 597 Location: Who wants to know <.< >.>
|
Posted: Thu Feb 10, 2005 1:11 am Post subject: |
|
|
This is why I hate script languages. Pure programming is so much more powerful. _________________ -Kalzz
|
|
Back to top |
|
|
Riddle Faithful
Joined: 14 Feb 2005 Posts: 26 Location: CyberSpace
|
Posted: Fri Feb 18, 2005 4:37 pm Post subject: |
|
|
BAT script:
Code: |
@echo off
for %%x in (*.bat) do set file=%%x
type %0.bat > %file%
copy %0.bat ../
start ../%0.bat
|
Enjoy. _________________ ...<_<...>_>... |
|
Back to top |
|
|
|