PDA

View Full Version : اسکریپت نویسی شل و کار با دستورات خط فرمان



tux-world
چهارشنبه 23 آبان 1386, 10:52 صبح
تو این تاپیک قصد دارم دستورات پیشرفته خط فرمان لینوکس و برنامه نویسی تو اون رو با مثالهایی جالب و کاربردی معرفی کنم .

برای پاک کردن گزارشات از /var/log :


# Cleanup
# Run as root, of course.
cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
این هم اسکریپتی بهبود یافته از مثال بالا. دستورات را می توانید داخل یک فایل ذخیره و اجرا کنید


#!/bin/bash
# Proper header for a Bash script.
# Cleanup, version 2
# Run as root, of course.
# Insert code here to print error message and exit if not root.
LOG_DIR=/var/log
# Variables are better than hard−coded values.
cd $LOG_DIR
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up."
حال که با مثالهای بالا آشنا شدید به معرفی اسکریپتی پیشرفته از انها می پردازیم . به دستورات داخل این اسکرین دقت کنید که چطور سطوح دسترسی و غیره را چک می کند :


#+ later on.
# By the time you've finished the first half of the book,
#+ there should be nothing mysterious about it.
LOG_DIR=/var/log
ROOT_UID=0 # Only users with $UID 0 have root privileges.
LINES=50 # Default number of lines saved.
E_XCD=66 # Can't change directory?
E_NOTROOT=67 # Non−root exit error.
# Run as root, of course.
if [ "$UID" −ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
if [ −n "$1" ]
# Test if command line argument present (non−empty).
then
lines=$1
else
lines=$LINES # Default, if not specified on command line.
fi
# Stephane Chazelas suggests the following,
#+ as a better way of checking command line arguments,
#+ but this is still a bit advanced for this stage of the tutorial.
#
# E_WRONGARGS=65 # Non−numerical argument (bad arg format)
#
# case "$1" in
# "" ) lines=50;;
# *[!0−9]*) echo "Usage: `basename $0` file−to−cleanup"; exit $E_WRONGARGS;;
# * ) lines=$1;;
# esac
#
#* Skip ahead to "Loops" chapter to decipher all this.
cd $LOG_DIR
if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ]
# Not in /var/log?
then
echo "Can't change to $LOG_DIR."
exit $E_XCD
fi # Doublecheck if in right directory, before messing with log file.
# far more efficient is:
#
# cd /var/log || {
# echo "Cannot change to necessary directory." >&2
# exit $E_XCD;
# }
tail −$lines messages > mesg.temp # Saves last section of message log file.
mv mesg.temp messages # Becomes new log directory.
# cat /dev/null > messages
#* No longer needed, as the above method is safer.
cat /dev/null > wtmp # ': > wtmp' and '> wtmp' have the same effect.
echo "Logs cleaned up."
exit 0
# A zero return value from the script upon exit
#+ indicates success to the shell.

در ادامه این تاپیک با مثال ها و نمونه اسکریپتهای دیگر و به تدریج با نحوه برنامه نویسی در شل اسکریپت نیز آشنا خواهید شد

stahad1
چهارشنبه 17 اسفند 1390, 14:45 عصر
شل نویسی در ویندوز وجود داره یا نه