리눅스 쉘 스크립트 문제

신민창·2021년 4월 3일
0

리눅스

목록 보기
10/11
  1. 디렉터리 자동 생성 프로그램
!/bin/bash

for i in $(seq 1 3)
do
mkdir /dir$i
echo "dir$i Created"
for j in $(seq 1 3)
do
mkdir /dir$i/subdir$j
echo "subdir$j Created"
done
done

exit 0
  1. 사용자 자동 추가 프로그램
#!/bin/bash

echo "Enter Username? : "
read name
echo "Start number : "
read number
echo "Count : "
read c
for i in $(seq 1 $c)
do
useradd $name`expr $number + $i`
echo "P@ssw0rd" | passwd --stdin $name`expr $number + $i`
echo "$name`expr $number + $i` and password created...."
done

exit 0
  1. 주요 파일 점검 프로그램
#!/bin/bash
# check file : test.txt

# Backup File Check
if [ -f test.txt.backup ] 
then
	# Backup file exist
	echo "Backup file found!"
else
	# Backup file doesn't exist
	cp test.txt test.txt.backup && echo "Backup File Created!"
	exit 0
fi

# File Check
if [ -f test.txt ] 
then
	# Original file exist 
	echo "Original file found!"
else
	# Original file doesn't exist
	echo "Original file not found!"
	cp test.txt.backup test.txt && echo "File Recovered!"
	exit 0
fi

# File contents Check
if diff test.txt test.txt.backup > /dev/null 2>&1
then
	# File same
	echo "File Check Complete!"
else
	# File not same
	echo "File Changed!"
	cp -f test.txt.backup test.txt && echo "File recovered!"
fi
  1. 시스템 상태 체크 프로그램
#!/bin/bash

# Infinity Loop
while true
do
	DATETIME=`date '+%y%m%d%H%M%S'`
	FILENAME="LOG_$DATETIME.log"

	echo "FILENAME=$FILENAME"

	echo "1. LOGGING TIME =====" >> $FILENAME
	date >> $FILENAME
	echo "2. VMSTAT =====" >> $FILENAME
	vmstat >> $FILENAME
	echo "3. PS -EF =====" >> $FILENAME
	ps -ef | head -10 >> $FILENAME
	echo "4. DF -H =====" >> $FILENAME
	df -h >> $FILENAME

	sleep 10
done
  1. 파일이름 일괄 변경 프로그램
#!/bin/bash

echo "Extention from : "
read a
echo "Extention to : "
read b
for i in *.$a
do
rename .$a .$b *.$a
done

exit o
  1. 휴지통 프로그램
#!/bin/bash
# ./myrm.sh [deletefile] : file delete
# ./myrm.sh -l : view recycle
# ./myrm.sh -r [filename] : recover file

# Recycle directory check
if [ -d /home/$USER/recycle ]
then
	:
else
	mkdir /home/$USER/recycle && echo "/home/$USER/recycle is your recycle"
fi

if [ $# -ge 3 -o $# -eq 0 ]
then
	echo "Usage: ./myrm [-l|-r] [filename]"
	exit 1
fi

if [ $1 = "-l" ]
then
	ls -l /home/$USER/recycle | sed '1d' | sed '/_path/d'	
elif [ $1 = "-r" ]
then
	if [ -f /home/$USER/recycle/$2 ]
	then
		RECOVERPATH=`cat /home/$USER/recycle/$2_path`
		mv /home/$USER/recycle/$2 $RECOVERPATH && echo "$2 file recovered"
		rm /home/$USER/recycle/$2_path && echo "information file deleted"
	else
		echo "$2 file is not in recycle"
	fi
elif [ -f $1 ]
then
	mv $1 /home/$USER/recycle && echo "File moved to recycle"
	pwd > /home/$USER/recycle/$1_path && echo "Deleted file information created"
else
	echo "Error"
	exit 1
fi
  1. 실행 중인 프로그램 중지 프로그램
#!/bin/bash
# 10719 pts/2    00:00:00 sleep

if [ $# -eq 1 ]
then
	:
else
	echo "Usage: ./killproc.sh [process name]"
	exit 1
fi

ps | grep $1 | awk '{ print $1 }' > /tmp/proclist

for PID in `cat /tmp/proclist`
do
	echo -n "PID $PID process found. Kill it? (K/T/C) : "; read ANSWER

	case $ANSWER in
		K|k) kill -9 $PID; echo "$PID killed" ;;
		T|t) kill -15 $PID; echo "$PID terminated" ;;
		*) echo "Canceled" ;;
	esac
done

if [ -f /tmp/proclist ]
then
	rm /tmp/proclist
fi

몇 개는 직접 만들어보고 몇 개는 직접 못 만들어 답을 참고하여 만들거나 답을 그대로 썼지만 코드를 복습하며 직접 만들지 못한 문제의 코드들을 이해하였다.

0개의 댓글