lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

os:  ubuntu2204 -- 配置和常用脚本

 

 

 

 

一、配置(/etc/profile)

 1 [wit@on:null]$ cat  /etc/profile
 2 # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
 3 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
 4 
 5 if [ "${PS1-}" ]; then
 6   if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
 7     # The file bash.bashrc already sets the default PS1.
 8     # PS1='\h:\w\$ '
 9     if [ -f /etc/bash.bashrc ]; then
10       . /etc/bash.bashrc
11     fi
12   else
13     if [ "$(id -u)" -eq 0 ]; then
14       PS1='# '
15     else
16       PS1='$ '
17     fi
18   fi
19 fi
20 
21 if [ -d /etc/profile.d ]; then
22   for i in /etc/profile.d/*.sh; do
23     if [ -r $i ]; then
24       . $i
25     fi
26   done
27   unset i
28 fi
29 [wit@on:null]$ 
30 [wit@on:null]$ 

 

 

 

 

二、配置( /etc/bash.bashrc )

 1 [wit@on:null]$ cat  /etc/bash.bashrc 
 2 # System-wide .bashrc file for interactive bash(1) shells.
 3 
 4 # To enable the settings / commands in this file for login shells as well,
 5 # this file has to be sourced in /etc/profile.
 6 
 7 # If not running interactively, don't do anything
 8 [ -z "$PS1" ] && return
 9 
10 # check the window size after each command and, if necessary,
11 # update the values of LINES and COLUMNS.
12 shopt -s checkwinsize
13 
14 # set variable identifying the chroot you work in (used in the prompt below)
15 if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
16     debian_chroot=$(cat /etc/debian_chroot)
17 fi
18 
19 # set a fancy prompt (non-color, overwrite the one in /etc/profile)
20 # but only if not SUDOing and have SUDO_PS1 set; then assume smart user.
21 if ! [ -n "${SUDO_USER}" -a -n "${SUDO_PS1}" ]; then
22   PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
23 fi
24 
25 # Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
26 # If this is an xterm set the title to user@host:dir
27 #case "$TERM" in
28 #xterm*|rxvt*)
29 #    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
30 #    ;;
31 #*)
32 #    ;;
33 #esac
34 
35 # enable bash completion in interactive shells
36 #if ! shopt -oq posix; then
37 #  if [ -f /usr/share/bash-completion/bash_completion ]; then
38 #    . /usr/share/bash-completion/bash_completion
39 #  elif [ -f /etc/bash_completion ]; then
40 #    . /etc/bash_completion
41 #  fi
42 #fi
43 
44 # sudo hint
45 if [ ! -e "$HOME/.sudo_as_admin_successful" ] && [ ! -e "$HOME/.hushlogin" ] ; then
46     case " $(groups) " in *\ admin\ *|*\ sudo\ *)
47     if [ -x /usr/bin/sudo ]; then
48         cat <<-EOF
49         To run a command as administrator (user "root"), use "sudo <command>".
50         See "man sudo_root" for details.
51 
52         EOF
53     fi
54     esac
55 fi
56 
57 # if the command-not-found package is installed, use it
58 if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
59         function command_not_found_handle {
60                 # check because c-n-f could've been removed in the meantime
61                 if [ -x /usr/lib/command-not-found ]; then
62                    /usr/lib/command-not-found -- "$1"
63                    return $?
64                 elif [ -x /usr/share/command-not-found/command-not-found ]; then
65                    /usr/share/command-not-found/command-not-found -- "$1"
66                    return $?
67                 else
68                    printf "%s: command not found\n" "$1" >&2
69                    return 127
70                 fi
71         }
72 fi
73 [wit@on:null]$ 
74 [wit@on:null]$ 

 

 

 

 

三、ubuntu 系统更新脚本(update)

 1 #!/usr/bin/env bash
 2 
 3 
 4 
 5 
 6 # file_name = update
 7 
 8 
 9 
10 
11 echo
12 echo -- update --
13 
14 echo "ROOT_MIMA" | sudo -S date;
15 echo "ROOT_MIMA" | sudo -S apt update -y;
16 echo "ROOT_MIMA" | sudo -S apt upgrade -y;
17 echo "ROOT_MIMA" | sudo -S apt update -y;
18 echo "ROOT_MIMA" | sudo -S apt autoclean -y;
19 echo "ROOT_MIMA" | sudo -S apt autoremove -y;
20 
21 echo -- update end --
22 echo

 

 

 

 

四、ubuntu 移除旧内核脚本(remove_old_kernel_ubuntu)

  1 #!/usr/bin/env bash
  2 
  3 
  4 
  5 
  6 # filename = remove_old_kernel_ubuntu
  7 
  8 
  9 
 10 
 11 # remove_old_kernel_ubuntu_help
 12 
 13 function remove_old_kernel_ubuntu_help()
 14 {
 15 
 16      echo
 17         echo
 18         echo -e  "\n\t== remove_old_kernel_ubuntu_help: begin =="
 19         echo -e  '  command                        parameter1              function'
 20         echo -e  '  remove_old_kernel_ubuntu       null                    -- List all kernel files installed.'
 21         echo -e  '  remove_old_kernel_ubuntu       kernel-old-version      -- Remove the kernel files matched with "kernel-old-version" .'
 22         echo
 23         echo -e  '  == examples =='
 24         echo -e  '  remove_old_kernel_ubuntu                               List all kernel files installed. '
 25         echo -e  '  remove_old_kernel_ubuntu       6.2.0-36        Remove kernel files matched with "6.2.0-36" .'
 26         echo -e  '  [wit@on:tools]$  ./remove_old_kernel_ubuntu  6.2.0-36'
 27         echo -e  "\t== remove_old_kernel_ubuntu_help: begin ==\n"
 28         echo
 29         echo
 30 
 31 
 32 }
 33 
 34 
 35 
 36 # display kernel files by current using
 37 
 38 function current_use_kernel()
 39 {
 40 
 41     echo -e  "\n\t-- current_use_kernel: begin --";
 42 
 43         msg=$(echo "ROOT_MIMA" | sudo -S uname -srmo)
 44         echo -e "\t\t" ${msg}
 45 
 46     echo -e  "\t-- current_use_kernel: end --\n";
 47 
 48 }
 49 
 50 
 51 
 52 
 53 # list kernel files and return a parameter of type of array.
 54 
 55 function list_kernel()
 56 {
 57 
 58     echo -e "\n\t-- list_kernel: begin --";
 59     
 60     i=1
 61     for f in $(echo "ROOT_MIMA" | sudo -S dpkg  --get-selections | grep -E "^linux" | awk '{ print $1}')
 62     do
 63         echo -e "\t\tLIST_FILE_$i := " ${f}
 64         i=$(($i+1))
 65     done
 66 
 67     echo -e "\t-- list_kernel: over --\n";
 68 
 69 }
 70 
 71 
 72 
 73 
 74 
 75 # delete files of old kernels
 76 
 77 function delete_old_kernel_files()
 78 {
 79 
 80 
 81     if [ $1 ]
 82     then
 83 
 84         echo -e "\n\t-- delete_old_kernel_files: begin --";
 85 
 86         echo -e "\n\t\tREMOVLE_OLD_KERNEL := " $1 "\n"
 87 
 88         kernel_files=$(echo "ROOT_MIMA" | sudo -S dpkg  --get-selections | grep -E "^linux.*$1-?*" | awk '{ print $1}')
 89 
 90         i=1
 91         for f in ${kernel_files}
 92         do
 93             echo -e "\n\n\t\tDELETE_FILE_$i := " ${f} "\n"
 94             echo "ROOT_MIMA" | sudo -S apt purge -y ${f}
 95             i=$(($i+1))
 96         done
 97 
 98         echo -e "\t-- delete_old_kernel_files: over --\n";
 99 
100     else
101 
102         echo -e "\t[ ERROR ]:\t" "\$1 is NULL, this programe is over !"
103         exit 3
104 
105     fi
106 
107 }
108 
109 
110 
111 
112 # fina_work():  remove old information of old kernel, and then clean, and then update-grub
113 
114 function final_work()
115 {
116 
117     echo -e "\n\t-- final_work: begin --\n";
118         #clean_files=$(echo "ROOT_MIMA" | sudo -S dpkg --get-selections | grep linux | grep reinstall | awk '{ print $1}')
119      clean_files=$(echo "ROOT_MIMA" | sudo -S dpkg --get-selections | grep linux | grep reinstall | awk '{ print $1}')
120 
121     if [ ${clean_files} ]
122     then
123         for f in ${clean_files}
124         do
125             echo echo "ROOT_MIMA" | sudo -S dpkg -P ${f}
126         done
127     fi
128 
129     echo "ROOT_MIMA" | sudo -S apt autoremove -y;
130     #echo "ROOT_MIMA" | sudo -S update-grub;
131 
132     echo -e "\n\t-- final_work: over --\n";
133 }
134 
135 
136 
137 
138 # run
139 
140 function run()
141 {
142     
143     echo    
144     echo -e "\n== remove_old_kernel_ubuntu: begin ==\n";
145 
146 
147         # remove_old_kernel_ubuntu_help
148             remove_old_kernel_ubuntu_help
149 
150         # display current kernel
151             current_use_kernel
152 
153         # list all files of kernel
154             list_kernel
155 
156         # delete all files of kernel
157         if [ $1 ]
158         then
159             delete_old_kernel_files $1
160 
161             # final_work
162             final_work
163         fi
164 
165         # list all files of kernel
166             list_kernel
167 
168         # display current kernel
169             current_use_kernel
170 
171     echo -e "\n== remove_old_kernel_ubuntu: over ==\n";
172     echo    
173 
174 }
175 
176 
177 
178 
179 run $1

 

 

 

 

五、参考文档

 

  1、os:ubuntu2204 -- 删除“旧内核”脚本  https://www.cnblogs.com/lnlidawei/p/17814188.html

 

posted on 2023-11-22 03:08  lnlidawei  阅读(99)  评论(0编辑  收藏  举报