## file.sh // 파일이 존재하면 파일 내용을 보여주고 아니면 없다고 출력
#!/bin/bash
if [ -f /home/$1/.plan ]; then
cat /home/$1/.plan
else
echo "User $1 is not make .plan file."
fi
-f 옵션 : 파일이 존재하는지 여부를 확인
# !ch
# ./file.sh ubuntu
User ubuntu is not make .plan file.
# cat > ~ubuntu/.plan // 파일 내용 넣기
I will go to travel this weekend.
^d
# ./file.sh ubuntu // 인자로 넣기
I will go to travel this weekend. // 파일 내용 출력
# cat >> ~ubuntu/.plan //파일 내용 수정
destination is anywhere..hmhm
^d
# ./file.sh ubuntu // 파일 내용 확인
I will go to travel this weekend.
destination is anywhere..hmhm
## dir.sh // 디렉토리가 존재하는지
#!/bin/bash
if [ -d $1 ]; then
echo "$1 directory is exit~!!"
else
echo "$1 directory is not exit~!!"
fi
// -d : 디렉토리 존재여부
# !ch
# ./dir.sh /work/shells
/work/shells directory is not exit~!!
# ./dir.sh /work/full/shells
/work/full/shells directory is exit~!!
## newerfile.sh // 어떤 게 더 최근 파일인지 확인해보기
#!/bin/bash
file1=$1
file2=$2
if [ $# -eq 2 ]; then
if [ $file1 -nt $file2 ]; then
echo "$file1 is newer file than $file2."
else
echo "$file2 is newer file than $file1."
fi
else
echo "Input two parameter..!!"
fi
# !ch
# touch test1 test2
// touch : 파일의 생성과 파일의 날짜, 시간을 변경하는 명령어 입니다 옵션 없이 사용할 경우 서버의 현재시간으로 변경
# ./newerfile.sh test1 test2
test2 is newer file than test1.
# touch test1
// test1 파일의 시간을 서버의 현재시간으로 변경
# ./newerfile.sh test1 test2
test1 is newer file than test2.
## logic.sh // 첫번째 파라미터가 test이거나 aaa면 good 아니면 bad 출력하기
#!/bin/bash
opt=$1
if [ $opt == 'test' -o $opt == 'aaa' ]; then
echo good
else
echo bad
fi
# !ch
# ./logic.sh test
good
# ./logic.sh aaa
good
# ./logic.sh bbb
bad
## logic_new.sh
#!/bin/bash
opt1=$1
opt2=$2
if [ $# -eq 2 ]; then
if [ $opt1 == 'test' -a $opt2 == 'aaa' ]; then
echo good
elif [ $opt1 == 'aaa' -a $opt2 == 'test' ]; then
echo good
else
echo bad
fi
else
echo "Input two parameters....!!"
fi
// $#은 파라미터의 수
// $# -eq 2 ==> 파라미터가 2개이다.
# ./logic_new.sh test aaa
good
# ./logic_new.sh test bbb
bad
# ./logic_new.sh aaa test
good
# ./logic_new.sh bbb test
bad
# ./logic_new.sh bbb
Input two parameters....!!
>> [ $opt1 == 'test' -a $opt2 == 'aaa' ] || [ $opt1 == 'aaa' -a $opt2 == 'test' ] 로 조건을 바꾸기도 가능
## useradd.sh // uid, 유저 수를 파라미터로 받아 유저를 생성한다.
#!/bin/bash
i=1
uid=$1
cnt=$2
while [ $i -le $cnt ]; do
let uid+=1
useradd -u $uid -g users -d /home/user$i -s /bin/bash -m user$i
passwd -d user$i
let i+=1
done
echo Complete!!
# !ch
# ./useradd.sh 2000 10
// 유저 uid 2000부터 10개 생성
# tail /etc/passwd
// 유저 생성 확인
## userdel.sh // 파라미터를 받아 user($첫번째 파라미터 반복)을 지운다.
#!/bin/bash
i=1
cnt=$1
while [ $i -le $cnt ]; do
userdel -r user$i
let i+=1
done
echo Complete!!
# ./userdel.sh 10
# tail /etc/passwd
확인
## test
The quick brown dog jumps over the lazy fox.
This is a test, this is only a test.
Romeo, Romeo!! Wherefore art thou Romeo?
## readfile.sh // 인덱스를 넣어서 파일 읽기
#!/bin/bash
count=1
cat $1 | while read line
do
echo "Line $count : $line"
count=$[ $count + 1 ]
done
echo "Finishing processing the file"
// read 라는 옵션을 쓰면 라인을 읽어줌. 거기에 while을 쓰면 한 줄씩 읽음.
# !ch
# ./readfile.sh
Line 1 : The quick brown dog jumps over the lazy fox.
Line 2 : This is a test, this is only a test.
Line 3 : Romeo, Romeo!! Wherefore art thou Romeo?
Finishing processing the file
## user.dat
romeo,3001,users,romeo test user
mica,3002,users,mica test user
andrea,3003,users,andrea test user
## useradd_file.sh // 파일을 읽어 구분자로 나눈 뒤 설정에 맞게 유저를 추가하는 파일
#!/bin/bash
input="user.dat"
while IFS=',' read -r username uid gid comment
do
echo "Adding $username"
useradd -u "$uid" -g "$gid" -c "$comment" -s /bin/bash -m "$username"
grep "$username" /etc/passwd
done < $input
IFS : Internal Field Separator // 구분자를 설정
username:pw:uid:gid:comment:home:shell --> 유저 생성하는 옵션
>> while IFS=',' read -r username uid gid comment
// 한 줄씩 읽는데 구분자를 ,로 설정해 ,를 기준으로 나눈다. 나눈 순서대로 username, uid, gid, comment 라는 변수로 설정한다.
# !ch
# ./useradd_file.sh // username을 출력
Adding romeo
Adding mica
Adding andrea
# tail -5 /etc/passwd
유저 생성되었는지 확인
## userdel_file.sh // 유저 없애는 옵션
#!/bin/bash
input="user.dat"
while IFS=',' read -r username uid gid comment
do
userdel "$username"
rm -rf /home/$username
rm -rf /var/mail/$username
echo "Delete $username"
done < $input
echo
tail -3 /etc/passwd
# !ch
# ./userdel_file.sh
유저 삭제
# head /etc/passwd
유저 삭제 확인
## user_list.sh // 유저 내용을 읽어서 출력
#!/bin/bash
input="/etc/passwd"
count=1
while IFS=':' read -r username pw uid gid comment home shell
do
echo "$count : $username - $uid - $gid - $home - $shell"
let count+=1
done < $input
// 구분자로 모두 나누기에 일단 변수로 다 추가 후 원하는 변수만 사용해야 한다.
# !ch
# ./user_list.sh
1 : root - 0 - 0 - /root - /bin/bash
2 : daemon - 1 - 1 - /usr/sbin - /usr/sbin/nologin
3 : bin - 2 - 2 - /bin - /usr/sbin/nologin
…..
## find_comm.sh // 두 파일의 같은 부분과 달라진 부분을 확인하게 하는 파일
#!/bin/bash
dir1=$1
dir2=$2
( cd $dir1; find . -maxdepth 1 -type f -print | sort ) > dir1-file.lst
( cd $dir2; find . -maxdepth 1 -type f -print | sort ) > dir2-file.lst
comm dir1-file.lst dir2-file.lst
# !ch
# ./find_comm.sh dir1 dir2
~~ (비슷한 명령)
# diff dir1-file.lst dir2-file.lst
## getopt.sh // 명령어 만들기 예제
#!/bin/bash
#
set -- $(getopt -q ab:cd "$@")
#
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option" ;;
esac
shift
done
#
count=1
for param in "$@"
do
echo "Parameter #$count : $param"
count=$[ $count + 1 ]
done
#
set -- $(getopt -q ab:cd "$@") --> a b 다음에 : 란 그 자리에 파라미터가 있다는 뜻
a b c d를 쓴 것만 default로 출력
shift 는 다음으로 넘긴다는 뜻
# ./getopt.sh -a -b test -c -d abc def
Found the -a option
Found the -b option, with parameter value 'test'
Found the -c option
-d is not an option
Parameter #1 : 'abc'
Parameter #2 : 'def'
# ./getopt.sh -a -b test -c -d -f -- abc def
Found the -a option
Found the -b option, with parameter value 'test'
Found the -c option
-d is not an option
Parameter #1 : 'abc'
Parameter #2 : 'def'
# ./getopt.sh -a -- abc def
Found the -a option
Parameter #1 : 'abc'
Parameter #2 : 'def'
while [ $# -ne 0 ]
==
while [ -n "$1" ]
## useradd_opt.sh // useradd 명령어 만들기
#!/bin/bash
#
set -- $(getopt -q u:g:c:d:s:k:m "$@")
#
echo
while [ $# -ne 0 ]
do
case "$1" in
-u) param="$2"
echo "-u (uid) option, parameter value : $param"
shift ;;
-g) param="$2"
echo "-g (gid) option, parameter value : $param"
shift ;;
-c) param="$2"
echo "-c (commet) option, parameter value : $param"
shift ;;
-d) param="$2"
echo "-d (home directory) option, parameter value : $param"
shift ;;
-s) param="$2"
echo "-s (shell) option, parameter value : $param"
shift ;;
-k) param="$2"
echo "-k (ininial script directory) option, parameter value : $param"
shift ;;
-m) echo "-m (make home directory) option" ;;
--) shift
break ;;
*) echo "$1 is not an option" ;;
esac
shift
done
#
count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count+1 ]
done
#
# ./useradd_opt.sh -u 1001 -g users -c User1 -d /home/user1 -s /bin/bash -k /home/ubuntu -m -- user100
-u (uid) option, parameter value : '1001'
-g (gid) option, parameter value : 'users'
-c (comment) option, parameter value : 'User1'
-d (home directory) option, parameter value : '/home/user1'
-s (shell) option, parameter value : '/bin/bash'
-k (initial scripts directory) option, parameter value : '/home/ubuntu'
-m (make home directory) option
Parameter #1 : 'user100'
'Linux' 카테고리의 다른 글
리눅스 11일차(2) - Paste command (0) | 2024.03.18 |
---|---|
리눅스 11일차(1) - Cut command (0) | 2024.03.18 |
리눅스 10일차(2) - Shell Script (0) | 2024.03.18 |
리눅스 10일차(1) - Shell Script (2) | 2024.03.15 |
Centos8 chrhonyd 설정 (0) | 2024.03.14 |