bash脚本编程 - 来管理服务器导航 [英] Bash Scripting - to manage server navigation
问题描述
我写了一些的bash脚本围绕应用中管理时,使生活更轻松。
I am writing some bash scripts to make life easier when managing around an application.
脚本真的只是目录切换到我这节省了大量的时间最常见的位置。
The scripts really just change directories to my most common locations which saves a huge amount of time.
目前我有我的剧本在我的用户.bash_profile中,这很好地工作。
Currently I have my script in my users .bash_profile and this works nicely.
即
#SHORTENED COMMANDS
alias flushBash='source ~/.bash_profile'
alias editBash='sudo nano /home/gtec/.bash_profile'
#DIRECTORIES
alias gost='cd /var/www/location/of/directory'
从命令行我只需要输入:GOST和我带到我所选择的目录中。现在,上面是好的,如果我使用服务器然而,最终我将需要系统管理者等,所以我想我会写一个脚本来帮助第三方沿着唯一的一个。
From command line I just type: gost and I am taken to the directory of my choice. Now the above is fine if I am the only one using the server however, eventually I will need sys admins etc. so I figured I would write a script to help third parties along.
所以我增加了一个脚本来我的bash:
So I added a script to my bash:
#SCRIPTS
alias go='. /home/me/scripts/go.sh'
和的想法是,用户类型:走出去进入命令行,然后返回一个选项列表
And the idea is that the user types: "go" into the command line, and it then returns a list of options.
这是我迄今为止:
#!/bin/bash
clear
echo "Where do you want to go? home, anc-p, anc-s, auto-p, auto-s"
read -p "Enter a location :" location
if [$location = home]
then
cd /home/gtec
else
echo "Noting to do"
fi
当我运行在它上面的输出:
When I run the above it outputs:
Where do you want to go? home, anc-p, anc-s, auto-p, auto-s
Enter a location :home
[home: command not found
Noting to do
当我输入家返回主页:命令未找到...
and when I type in "home" it returns: home:command not found...
我我是做错了?
推荐答案
根据伊坦赖斯纳评论更新的答案,原来不准确的1 $ P $下面pserved。
主要的问题是语法:你需要的,如果周围的方括号的空间。此外,你应该总是给你的变量的情况下,它们包含空格字符。解决方法:
The main problem is syntactical: you need spaces around the square brackets of the if
. Furthermore, you should always quote your variables in case they contain whitespace characters. Solution:
#!/bin/bash
clear
echo "Where do you want to go? home, anc-p, anc-s, auto-p, auto-s"
read -p "Enter a location : " location
if [ "$location" = "home" ]
then
cd /home/gtec
else
echo "Nothing to do"
fi
原件,以供参考不准确的回答是:
Original, inaccurate answer for reference:
您在您若2错误:首先,你跟 =
分配,而不是与测试==
,其次你错过的空白。下面是它应该是什么样子:
You have 2 mistakes in your if: firstly, you assign with =
instead of testing with ==
, secondly you're missing whitespace. Here's what it should look like:
#!/bin/bash
clear
echo "Where do you want to go? home, anc-p, anc-s, auto-p, auto-s"
read -p "Enter a location :" location
if [ $location == home ]
then
cd /home/gtec
else
echo "Nothing to do"
fi
的误差(1)表明谎称 ==
是唯一的选择;(2)省略了双引号。
The inaccuracy lied in (1) suggesting that ==
is the only option and (2) omitting the double quotes.
这篇关于bash脚本编程 - 来管理服务器导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!