Shell脚本太棒了
(非常感谢Ben Sinclair 的评论,帮助我纠正了一些问题。)
我喜欢使用命令行,它的简洁性和强大功能都让我着迷。
我只是想分享一个小技巧,希望能帮您提高效率。也希望您能分享一些您觉得好用又实用的工具!
以下是我最喜欢的。
别名非常有用,我有很多。如果你想要,可以私信我。如果反响不错,我会专门写篇文章介绍一下。不过,这篇文章主要侧重于函数和模块的组合,用来做一些很酷的事情。
首先,写这篇文章的起因是我刚才突然想到“为什么我要每次都重新输入一遍!”,然后我花了 10 秒钟写了一个简单的函数,意识到我应该分享一下!
所以在工作中,我们会创建 git 分支,然后根据不同的故事创建分支。
我个人将源/主分支称为“上游”,而将我的分支称为 name。
每次开始写新故事时,我都需要运行以下命令。
git fetch upstreamgit checkout -b STORY_NUMBER_WITH_BRIED_DESC upstream/mastergit pull upstream master// 虽然重复,但不知为何 fetch 并不总是能为我更新
所以我每次开始写新故事的时候都要输入这些内容。这很费劲,尤其是当涉及到多个服务的时候。
因此,我 ~/.zshrc 文件中的这个小函数(您可能使用的是 .bash_profile)让一切变得更简单!
newStory() {
git fetch upstream && git checkout -b $1 upstream/master && git pull upstream master;
}
要使用它,只需确保注明你的新更改来源source ~/.zshrc,现在我只需输入
newStory B-23168_automated_testing
我还使用许多其他一次性的 shell 脚本函数。我记不住单词的拼写,所以我写了一个spl命令,它会在后台运行aspell模块。你可以通过 brew 安装这个模块。
spl () {
aspell -a <<< "$1"
}
现在我运行新命令,它会返回它认为我想要表达的意思。前一两个词几乎总是你想要表达的意思。
spl polymorfus
> & polymorfus 3 0: polymorphous, polymers, polymer's
也许你想创建一个自定义函数,调用一个自定义脚本来完成一些你在 Bash 中无法完成的任务。也许 PHP 才是你最擅长的语言,而你除了 Bash 的基础知识之外,其他方面都一窍不通?
没问题!
添加你的 PHP shell 脚本。很简单,只需使用 `<php-shell-script>` 命令#!/path/to/php,然后输入你的 PHP 脚本即可。你可以使用 `<php-shell-script>` 命令获取路径。which php
~/.settings/snippets/urldecode(无扩展名)
#!/usr/bin/php
<?php
echo urldecode($argv[1]);
?>
现在将你的代码片段文件夹导出内容添加到文件末尾~/.zshrc。这样,该文件夹中的所有脚本都可以通过命令行调用!(感谢 Ben 提供的更佳方案)
export PATH=$HOME/.settings/snippets:$PATH
砰!你就有了一个属于你自己的新剧本!
urldecode this%20is%20my%20string%21%20
> this is my string! %
如你所见,bash 功能强大,用途广泛。它真是太棒了!下面我列出了一些你可能用得上的例子。如果其中使用了其他函数名,你可能需要使用 brew 或 npm 安装它。
# take every file and move it to the current folder
#
# with in parent folder you want, run flattenSubfolders
flattenSubfolders () {
find . -type f -exec mv -n -- {} . \;
}
# used after you use flattenSubfolders to remove empty subfolders
#
# with in parent folder you want, run removeEmptySubdirs
removeEmptySubdirs () {
find . -depth -mindepth 1 -type d -empty -exec rmdir {} \;
}
# will open chrome and turn your README.md in to a nice web view, requires https://github.com/joeyespo/grip
#
# rmd ./README.md
rmd () {
grip -b $1 &
TASK_PID=$!
sleep 10
kill $TASK_PID
}
# video helpers
#
# genWebMp4 ./movie.ext ./newmovie.mp4
genWebMp4 () {
ffmpeg -i $1 -vcodec h264 -acodec aac -strict -2 $2
}
#
# genWebm ./movie.ext ./newmovie.webm
genWebm () {
ffmpeg -i $1 -c:v libvpx-vp9 -b:v 2M -pass 1 -c:a libopus -f webm /dev/null && \
ffmpeg -i $1 -c:v libvpx-vp9 -b:v 2M -pass 2 -c:a libopus $2
}
#
# wavToMp3 ./file
wavToMp3() {
ffmpeg -i $1.wav -codec:a libmp3lame -qscale:a 2 $1.mp3
}
#
# with in folder of all mp3s, run mp3ToOgg
mp3ToOgg() {
for file in *.mp3
do ffmpeg -i "${file}" "${file/%mp3/ogg}"
done
}
#
# genVideoThumb ./video.ext ./thumbnail.ext
# you can use jpg and png for thumbnail ext
genVideoThumb () {
ffmpeg -ss 00:00:01 -i $1 -vframes 1 -q:v 2 $2
}
#
# spl word
spl () {
aspell -a <<< "$1"
}
#
# gitCleanBranches
gitCleanBranches() {
git branch --merged | egrep -v "(^\*|master|develop)" | xargs git branch -d
}
希望你喜欢。发现问题了吗?请告诉我。
请在评论区补充你的想法!
Varymade LLC。
我们目前的项目是https://charactergenerator4000.com和https://coder.exchange。欢迎访问并告诉我们您的想法。
——德尚
文章来源:https://dev.to/dechamp/bash-scripts-are-awesome-13m3