这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 48 additions & 15 deletions scripts/chsh.in
Original file line number Diff line number Diff line change
@@ -1,44 +1,77 @@
#!/bin/sh
# shellcheck shell=dash

set -e -u
set -eu

show_usage () {
is_executable_file() {
test -f "$1" -a -x "$1"
}

show_usage() {
echo "usage: chsh [-s shell]"
echo "Change the login shell."
echo
if [ "${1:-}" = "login" ]; then
echo "The shell value must be one of following and cannot be 'login':"
else
echo "The shell value must be one of following:"
fi
echo " - Empty value to restore default shell."
echo " - Absolute path to shell starting with a '/'."
echo " - Relative path to shell not starting with a '/' relative to '@TERMUX_PREFIX@/bin'."
}

set_shell () {
if [ "$1" = login ]; then
echo "login is not a valid shell"
set_shell() {
if [ -z "${1:-}" ]; then
echo "Restoring default shell"
rm -f "$HOME/.termux/shell"
exit 0
fi

if [ "$1" = "login" ]; then
show_usage "login"
exit 1
fi
mkdir -p $HOME/.termux
NEW_SHELL=@TERMUX_PREFIX@/bin/$1
if test -x $NEW_SHELL -a ! -d $NEW_SHELL; then
ln -f -s $NEW_SHELL $HOME/.termux/shell
else
echo "$NEW_SHELL is not an executable file!"
mkdir -p "$HOME/.termux"

unset NEW_SHELL
case "$1" in
"/"*) NEW_SHELL="$1" ;;
*) NEW_SHELL="@TERMUX_PREFIX@/bin/$1" ;;
esac

SHELL_TARGET="$(realpath -s "$NEW_SHELL")"

if ! is_executable_file "$SHELL_TARGET"; then
echo "The shell file '$SHELL_TARGET' is not an executable file." 1>&2
exit 1
fi

if [ "$SHELL_TARGET" -ef "@TERMUX_PREFIX@/bin/login" ]; then
echo "The shell file '$SHELL_TARGET' must not point to the '@TERMUX_PREFIX@/bin/login' script." 1>&2
exit 1
fi

ln -sf "$SHELL_TARGET" "$HOME/.termux/shell"
}

O=`getopt -l help -- hs: "$@"`
eval set -- "$O"
while true; do
case "$1" in
-h|--help) show_usage; exit 0;;
-s) set_shell $2; exit 0;;
-s) set_shell "$2"; exit 0;;
--) shift; break;;
*) echo Error; show_usage; exit 1;;
esac
done

DEFAULT_SHELL=bash
if [ ! -x @TERMUX_PREFIX@/bin/$DEFAULT_SHELL ]; then DEFAULT_SHELL=sh; fi
if ! is_executable_file "@TERMUX_PREFIX@/bin/$DEFAULT_SHELL"; then DEFAULT_SHELL=sh; fi

echo Changing the login shell
echo Enter the new value, or press ENTER for the default
printf " Login Shell [$DEFAULT_SHELL]: "
read shell

if [ -z "$shell" ]; then shell=$DEFAULT_SHELL; fi
set_shell $shell
set_shell "$shell"
39 changes: 30 additions & 9 deletions scripts/login.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
#!/bin/sh
# shellcheck shell=dash

is_executable_file() {
test -f "$1" -a -x "$1"
}

set_default_shell() {
for file in "@TERMUX_PREFIX@/bin/bash" "@TERMUX_PREFIX@/bin/sh" "/system/bin/sh"; do
if is_executable_file "$file"; then
SHELL="$file"
break
fi
done
}

if tty >/dev/null 2>&1 && [ $# = 0 ] && [ ! -f ~/.hushlogin ] && [ -z "$TERMUX_HUSHLOGIN" ]; then
# Use user defined dynamic motd file if it exists
Expand All @@ -19,17 +33,24 @@ if tty >/dev/null 2>&1 && [ $# = 0 ] && [ -f @TERMUX_PREFIX@/etc/motd-playstore
printf '\033[0;31m'; cat @TERMUX_PREFIX@/etc/motd-playstore; printf '\033[0m'
fi

if [ -G ~/.termux/shell ]; then
export SHELL="`realpath ~/.termux/shell`"
else
for file in @TERMUX_PREFIX@/bin/bash @TERMUX_PREFIX@/bin/sh /system/bin/sh; do
if [ -x $file ]; then
export SHELL=$file
break
fi
done
unset SHELL
SHELL_FILE=~/.termux/shell

if [ -L "$SHELL_FILE" ]; then
# Set the symlink's value as the shell target.
SHELL_TARGET="$(readlink "$SHELL_FILE")"
if is_executable_file "$SHELL_TARGET" &&
[ ! "$SHELL_TARGET" -ef "@TERMUX_PREFIX@/bin/login" ]; then
SHELL="$SHELL_TARGET"
fi
fi

if [ -z "${SHELL:-}" ]; then
set_default_shell
fi

export SHELL

# TERMUX_APP_PACKAGE_MANAGER should be exported by termux-app v0.119.0+ itself
if [ -z "${TERMUX_APP_PACKAGE_MANAGER-}" ]; then
if { [ -n "$(command -v dpkg)" ] && dpkg --compare-versions "$TERMUX_VERSION" lt 0.119.0; } || # apt
Expand Down