我所拥有的是一些用于我公司不同build筑物的crons的脚本文件,但是我所遇到的是我不得不进入每个文件并将OAK3更改为不同的build筑物ID,以及橡木3(小写)。 这些文件都位于各自的仓库文件夹ex: Desktop/CRON/OAK3 。 我想要做的是,如果是OAK3使用OAK3 ,而使用oak3 (小写),而不是每次为仓库创build一个新数据库时都必须进入每个文件。
我是新的Linux世界,所以我不知道如果有一种方法,并没有发现任何东西在谷歌。
Example.sh
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test /workplace/gwwallen/ETLdump/OAK3/oak3_count_portal.txt --ignore-lines=1
希望的效果是可能的
用适当的时间方式“find-ls”
Bash脚本在terminal中手动运行,但不从crontab执行
eval就像是声明条件一样
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test /workplace/gwwallen/ETLdump/$WAREHOUSE_ID/$warehouse_id_count_portal.txt --ignore-lines=1
EL 5中/etc/init.d/functions中声明的含义
是否有可能自动FLO STDOUT / STDERR没有TTY?
grep以每行为单位显示模式的发生
如果我得到你想要的,我不确定,这将有助于做所有新的数据库
databases=`MysqL -B -r -u ${user} --skip-column-names -p${pass} --execute='show databases'` for db in $databases; do ## Now loop through the above array echo $db # current DB MysqLdump -u $user --password=$pass $db > "$db.sql" #dump db to file done
在Bash特殊变量$0使用dirname和basename的组合,可以获得所有你需要的东西。
正在运行的脚本的文件名是$0 。 同时dirname $0会给你执行文件的目录路径。 但是你不想要完整的路径,只需要最后一部分, basename将提供。 realpath用于展开目录. 未使用。
$ ls tmp.sh # Ok,there's our file $ dirname tmp.sh . # The . is current directory $ dirname $(realpath tmp.sh) /home/mjb/OAK3 # so we expand it with realpath $ basename $(dirname $(realpath tmp.sh)) OAK3 # then take only the last one with basename
所以下面是它将如何为你工作:
# Get the directory name warehouse=$(basename $(dirname $(realpath $0))) # And lowercase it with `tr` into a new variable warehouse_lcase=$(echo $warehouse | tr '[:upper:]' '[:lower:]') # Substitute the variables /usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test /workplace/gwwallen/ETLdump/${warehouse}/${warehouse_lcase}_count_portal.txt --ignore-lines=1
另请参阅: Bash脚本可以告诉它存储在哪个目录中?
有很多简单的方法来找出当前工作目录的基名: pwd -PL | sed sg.*/ggg – pwd -PL | sed sg.*/ggg pwd -PL | sed sg.*/ggg
[san@alarmp OAK3]$ pwd; pwd -PL | sed sg.*/ggg /opt/local/OAK3 OAK3
所以,如果我正确地理解了你的要求,如果你不想手动改变脚本,你可以在这个特定的目录中进行:
$ cat example.sh /usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test /workplace/gwwallen/ETLdump/OAK3/oak3_count_portal.txt --ignore-lines=1 # $ this_dir=$(pwd -PL | sed sg.*/ggg) # $ sed -e "s/${this_dir}/${WAREHOUSE_ID}/g" example.sh /usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test /workplace/gwwallen/ETLdump/${WAREHOUSE_ID}/oak3_count_portal.txt --ignore-lines=1 # $ sed -e "s/$(echo $this_dir | tr '[:upper:]' '[:lower:]')/${warehouse_id}/g" example.sh /usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test /workplace/gwwallen/ETLdump/OAK3/${warehouse_id}_count_portal.txt --ignore-lines=1
使用-i选项使更改永久保存在文件中(不创建新文件),如下所示:
sed -ie "s/${this_dir}/${WAREHOUSE_ID}/g" example.sh
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。