【问题标题】:Linux Bash file use Directory nameLinux Bash 文件使用目录名
【发布时间】:2014-01-21 19:54:04
【问题描述】:

我有一些脚本文件,用于我公司不同建筑物的 crons,但我遇到的是我必须进入每个文件并将 OAK3 更改为不同的建筑物id,以及 Oak3(小写)。这些文件都位于相应的仓库文件夹中,例如:Desktop/CRON/OAK3。我想做的是,如果它是OAK3,则使用OAK3oak3(小写),而不是每次为仓库创建新数据库时都必须进入每个文件。

我是 linux 世界的新手,所以我不确定是否有办法,也没有在 google 上找到任何东西。

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

想要的效果是可能的

/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

【问题讨论】:

  • 如果我理解,您想根据 目录 名称 OAK3 执行此操作,或者您想取消使用目录来分隔它们?

标签: mysql linux bash cron


【解决方案1】:

如果我得到你想要的,我不确定,这将有助于创建所有新数据库

  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

【讨论】:

    【解决方案2】:

    dirnamebasename 与Bash 特殊变量$0 结合使用,您可以获得所需的一切。

    运行脚本的文件名为$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
    

    另请参阅:Can a Bash script tell which directory it's stored in?

    【讨论】:

    • 这看起来应该可以了,等我回去工作再试试!谢谢
    • @Spartacus38 你有机会尝试一下吗?
    • 这很好用!!!!!!谢谢。必须复制/粘贴新 ID 很痛苦。
    • @Spartacus38 太好了,很乐意提供帮助。
    • 如何从cron运行?这是否保存在自己的文件中,可执行文件,以及从 cron 调用的文件?如果是这样,那应该可以按预期工作。如果它什么都不做,请检查 cron 日志,验证执行权限。如果 vars 没有成功,cron 将报告导入文件位置的错误,并且您会收到通知。
    【解决方案3】:

    有很多更简单的方法可以找出当前工作目录的基本名称: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 
    

    【讨论】:

    • sed 命令是一个非常有用的工具,但是在这种情况下,"dirname /bin/pwd" 或 "dirname $path_variable" 是比拔出 sed 锤更好的解决方案。
    猜你喜欢
    • 2017-01-17
    • 2014-12-15
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2016-06-25
    • 2016-08-01
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多