树莓派更换国内源脚本

每次树莓派刷系统后,安装许多大一点的软件,使用默认的官方源速度都是特别的慢,比如安装qt。。。。。。

然后我们一般就更换国内源,如阿里、清华等等,可是每次安装太麻烦了,还需要百度源链接。
这里附上国内常用的源链接:
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/

百度查找看有没有类似脚本直接运行./run.sh就能更换源,那岂不是非常舒服,结果真的找到了,脚本代码也挂github可下载。

csdn链接https://blog.csdn.net/baidu_26678247/article/details/103232369

github地址https://github.com/jiangtao008/raspi_source_change

附上脚本代码,可以直接拷贝运行:

#!/bin/bash
:<<EOF
Change the source of RasoberryPi
EOF

path_main=$(dirname $(readlink -f $0))
path_source_software="/etc/apt/sources.list"
path_source_system="/etc/apt/sources.list.d/raspi.list"
path_source_software_config="${path_main}/config/software.source."
path_source_system_config="${path_main}/config/system.source."
path_website_list="${path_main}/config/website.list"

echo "Start executing the script."
# TODO: Get the system version code
echo "Step 1: Get the system version code."
version_code=`cat /etc/os-release | grep 'VERSION_CODENAME'`
echo $version_code
if [[ $version_code =~ "buster" ]]; then
  path_source_software_config="${path_source_software_config}buster"
  path_source_system_config="${path_source_system_config}buster"
# elif [[ $version_code =~ "stretch" ]]; then
#   echo "[Warn] The system is not up-to-date, it is recommended to update"
#   path_source_software_config="${path_source_software_config}stretch"
#   path_source_system_config="${path_source_system_config}stretch"
# elif [[ $version_code =~ "jessie" ]]; then
#   echo "[Warn] The system is not up-to-date, it is recommended to update"
#   path_source_software_config="${path_source_software_config}jessie"
#   path_source_system_config="${path_source_system_config}jessie"
else
echo "[Error] Unsupported system version code. Exit!"
exit 1
fi

# TODO: Determine network.
echo "Step 2: Determine whether to connect to the network?"
ping -c 1 114.114.114.114 > /dev/null 2>&1
if [ ! $? -eq 0 ]; then
  echo "    [Error] The network is not connected. Please continue after connecting!"
  exit 1
else
  echo "    [OK] The network is connected."
fi

# TODO: Determine if a profile exists.
echo "Step 3: Determine if a profile exists."
if ! [ -f "${path_website_list}" ]
then
  echo "    [Not Exist] website list file: ${path_website_list}"
  exit 1
else
  echo "    [Exist] website list file: ${path_website_list}"
fi

if ! [ -f "${path_source_software_config}" ]
then
  echo "    [Not Exist] software source file: ${path_source_software_config}"
  exit 1
else
  echo "    [Exist] software source file: ${path_source_software_config}"
fi

if ! [ -f "${path_source_system_config}" ]
then
  echo "    [Not Exist] system source file: ${path_source_system_config}"
  exit 1
else
  echo "    [Exist] system source file: ${path_source_system_config}"
fi

# TODO: Select the source that needs to be replaced.
echo "Step 4: Select the source that needs to be replaced."
echo "Source List (In getting information, Please wait a moment):"
# Read ini file sections
# input : 
#   para1: file path
# return: section list
readIniSections() {
    file=$1;
    val=$(awk '/\[/{printf("%s ",$1)}' ${file} | sed 's/\[//g' | sed 's/\]//g')
    echo ${val}
}

# Read ini file val
# input :
#   para1: file path
#   para2: section name
#   para3: item name
# return item value
readIni() {
    file=$1;section=$2;item=$3;
    val=$(awk -F '=' '/\['${section}'\]/{a=1} (a==1 && "'${item}'"==$1){a=0;print $2}' ${file}) 
    echo ${val}
}

website_infos=`readIniSections $path_website_list`
website_infos_array=($website_infos)
list_index=0
printf "    |%-6s |%-16s |%-16s |%-16s |%-16s |%-10s\r\n" number name http_code time_connect speed_download url
for var in $website_infos
do
  website_val=`readIni $path_website_list $var website`
  #domain=`echo ${website_val} | awk -F'[/:]' '{print $4}'`
  result=`curl -m 5 -o /dev/null -s -w %{http_code}:%{time_connect}:%{speed_download} $website_val`
  result_array=(${result//:/ })  
  printf "    |%-6s |%-16s |%-16s |%-16s |%-16s |%-10s\r\n" $list_index $var ${result_array[0]} ${result_array[1]} ${result_array[2]} $website_val
  list_index=`expr $list_index + 1`
done
result_name="raspbian"
while true
do
  read -p "Please enter the number where the source needs to be replaced (Default 0): " result
  if [ -z $result ];then
    result=0
  fi
  ipt_val=`echo $result |sed 's/[-0-9]//g'`
  if [ ! -z $ipt_val ];then
    echo "[ERROR] The input is wrong, Please enter a pure number!"
  elif [ $result -gt `expr $list_index - 1` ];then
    echo "[ERROR] The input is wrong, Number out of range!"
  else
    result_name=${website_infos_array[result]}
    echo "Replacing a source named : " $result_name
    break
  fi
done
result_url=`readIni $path_website_list $result_name website`
result_url=(${result_url//\//\\\/}) 
result_urlpi=`readIni $path_website_list $result_name websitepi`
result_urlpi=(${result_urlpi//\//\\\/}) 
#echo $result_url
#exit 1

# TODO: Replace with the selected source.
echo "Step 5: Replace with the selected source."
# Back up the original file
if ! [ -f "${path_source_software}.bak" ]
then
  echo "    [Not Exist] software source original file, Back up files"
  sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
else
  echo "    [Exist] software source original file: ${path_source_software}.bak"
fi

if ! [ -f "${path_source_system}.bak" ]
then
  echo "    [Not Exist] system source original file, Back up files"
  sudo cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak
else
  echo "    [Exist] system source original file: ${path_source_system}.bak"
fi
echo "sudo sed -e \"s/source_url/${result_url}/g\" \"$path_source_software_config\" > ${path_source_software}" | sudo bash
echo "sudo sed -e \"s/source_url/${result_urlpi}/g\" \"$path_source_system_config\" > ${path_source_system}" | sudo bash

# TODO: Update the list of sources.
echo "Step 6: Update the list of sources."
echo "    Please wait a minute to update the source list."
sudo apt-get update #>/dev/null 2>&1
read -p "Do you want to perform the update? It will take some time to execute (yes/no, default: no): " result
if [[ $result == "yes" || $result == "y" ]]; then
  echo "Start the update task (Press Ctrl+C to exit)."
  sudo apt-get upgrade -y #>/dev/null 2>&1
  sudo apt-get dist-upgrade 
else 
  echo "Upgrade action cancellation"
fi
echo "[OK] The source was replaced successfully."

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值