如果按下向上/向左箭头键,是否可以在 bash 脚本中使用大小写箭头键来运行一组命令,如果按下向下/向右箭头键,是否可以运行一组命令?我试图通过在显示数据时使用箭头键在用户之间快速切换,使用此脚本从中读取数据。
function main() # The main function that controls the execution of all other functions
{
mkdir -p ~/usertmp # Make a new temporary user directory if it doesn't exist
touch ~/last_seen_output.txt # Create the output file if it doesn't exist
cat /dev/null > ~/last_seen_output.txt # Make sure that the output file is empty
gather # Call the "gather" function
total=$((`wc -l ~/usertmp/user_list.txt|awk '{print $1}'`-1)) # Calculate the total amount of lines and subtract 1 from the result
echo Current Time: `date +%s` > ~/last_seen_output.txt # Print the current time to the output file for later reference
echo "" > ~/last_seen_output.txt # Print a blank line to the output file
if [ $log -eq 1 ]
then
# If it is enabled, then delete the old backups to prevent errors
while [ $line_number -le $total ]
do
line_number=$((line_number+1)) # Add 1 to the current line number
calculate # Call the "calculate" function
hms # Call the "hms" function to convert the time in seconds to normal time
log
done
else
while [ $line_number -le $total ]
do
line_number=$((line_number+1)) # Add 1 to the current line number
calculate # Call the "calculate" function
hms # Call the "hms" function to convert the time in seconds to normal time
echo "Displaying, please hit enter to view the users one by one."
read # Wait for user input
if [ "$log_while_displaying" ]
then
log
display
else
display
fi
done
fi
}
https://github.com/jbondhus/last-seen/blob/master/last-seen.sh是完整的脚本。
注释为“等待用户输入”的读取命令是您按回车键转到下一个用户的命令。基本上,该脚本的作用是列出用户以及自每个用户登录以来经过的时间。我正在尝试使用箭头键在显示的每个用户之间切换。我认为可以使用 case 语句来区分关键输入。重申我的观点,我不确定这是否可能。如果不是,谁能想到另一种方法来做到这一点?