在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 是完整的脚本。read命令被注释为“等待用户输入”,它是您按Enter键以继续下一个用户的命令。基本上,此脚本会列出用户及其登录后经过的时间。我正试图使用箭头键在每个用户之间进行切换。我想这可能可以使用case语句来处理键盘输入。重申一下,我不确定这是否可行。如果不行,有人能想到其他方法吗?
-t 0.001
)来支持OP没有要求的东西。这是一项简单的增强功能,但引入了另一个可能发生故障的维度,似乎并没有使解决方案更加完美:shrug: - JellicleCat