【问题标题】:Bash Script for find a Script in a path and launch that another script用于在路径中查找脚本并启动另一个脚本的 Bash 脚本
【发布时间】:2014-11-18 08:53:52
【问题描述】:

看,我有这个脚本,它用于查找,一个“路径”内的脚本,然后启动另一个名为“.Iniciar”的脚本

#!/bin/sh

# La Funcion de este Script es encontrar el directorio
# Real donde se encuentra el programa

# La Version Original es de :
# 17 de Febrero del 2000 - Sam Lantinga, Loki Entertainment Software

# Esta Version Ha sido Traducida por
# Inukaze De Venezuela
# Sitio : http://inukaze.wordpress.com

Encontrar_Ruta()
{
    ruta_completa="`echo $1 | grep /`"
    if [ "$ruta_completa" = "" ]; then
        oIFS="$IFS"
        IFS=:
        for path in $PATH
        do if [ -x "$path/$1" ]; then
               if [ "$path" = "" ]; then
                   path="."
               fi
               ruta_completa="$path/$1"
               break
           fi
        done
        IFS="$oIFS"
    fi
    if [ "$ruta_completa" = "" ]; then
        ruta_completa="$1"
    fi

    if [ -L "$ruta_completa" ]; then
        ruta_completa=`ls -l "$ruta_completa" |sed -e 's/.* -> //' |sed -e 's/\*//'`
    fi
    dirname $ruta_completa
}

if [ "${RUTA_DEL_SOFTWARE}" = "" ]; then
    RUTA_DEL_SOFTWARE="`Encontrar_Ruta $0`"
fi

LD_LIBRARY_PATH=.:${RUTA_DEL_SOFTWARE}:${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH

if [ -x "${RUTA_DEL_SOFTWARE}/.Iniciar" ]
then
    cd "${RUTA_DEL_SOFTWARE}/"
    exec "./.Iniciar" $*
fi
echo "No Puedo ejecutar este Software. Esta bien escrito el Script de Inicio?"
exit 1

好的,它适用于没有空格的路径,嗯,我试图从 路径“/media/Compartido/Juegos/Emuladores & Roms/MS-D.O.S/Aladdin”

例如,当我的终端位于“桌面”文件夹中时

$ cd $HOME/Desktop
$ sh "/media/Compartido/Juegos/Emuladores & Roms/MS-D.O.S/Aladdin"/Iniciar
No Puedo ejecutar este Software. Esta bien escrito el Script de Inicio?

我无法启动它

但如果我将文件夹复制到“/media/Shared/Games/MS-D.O.S/Aladdin”

[ inukaze | 23-09-2014 | 08:55 pm ]
[Desktop]$ sh "/media/Shared/Games/MS-D.O.S/Aladdin"/Iniciar

Encontrado el Archivo de Configuracion para : Aladdin

DOSBox version 0.74
Copyright 2002-2010 DOSBox Team, published under GNU GPL.
---
CONFIG:Loading primary settings from config file Aladdin.conf
MIXER:Got different values from SDL: freq 22050, blocksize 256
ALSA:Can't subscribe to MIDI port (65:0) nor (17:0)
MIDI:Opened device:none
Two or more joysticks reported, initializing with 2axis
Using joystick USB Gamepad  with 2 axes, 10 buttons and 0 hat(s)
Using joystick Xbox Gamepad (userspace driver) with 6 axes, 11 buttons and 1 hat(s)

并尝试从原始位置使用“zsh”

[ inukaze | 23-09-2014 | 08:58 pm ]
[Desktop]$ zsh "/media/Compartido/Juegos/Emuladores & Roms/MS-D.O.S/Aladdin"/Iniciar

Encontrado el Archivo de Configuracion para : Aladdin

DOSBox version 0.74
Copyright 2002-2010 DOSBox Team, published under GNU GPL.
---
CONFIG:Loading primary settings from config file Aladdin.conf
MIXER:Got different values from SDL: freq 22050, blocksize 256
ALSA:Can't subscribe to MIDI port (65:0) nor (17:0)
MIDI:Opened device:none
Two or more joysticks reported, initializing with 2axis
Using joystick USB Gamepad  with 2 axes, 10 buttons and 0 hat(s)
Using joystick Xbox Gamepad (userspace driver) with 6 axes, 11 buttons and 1 hat(s)

这也可以,但我想从 bash / sh 中运行

感谢您的帮助。

【问题讨论】:

    标签: linux bash shell sh zsh


    【解决方案1】:

    如果带有空格的路径导致总是的问题意味着您没有使用足够的引用。

    具体来说,您正在使用可以包含空格而不“引用”它们的变量。

    例如在下面一行中。

    RUTA_DEL_SOFTWARE="`Encontrar_Ruta $0`"
    

    你没有引用$0。因此,当您运行sh "/media/Compartido/Juegos/Emuladores & Roms/MS-D.O.S/Aladdin"/Iniciar 尽管在上面的行中使用该路径(如$0)时引用路径作为参数时,shell 看到的是:

    RUTA_DEL_SOFTWARE="`Encontrar_Ruta '/media/Compartido/Juegos/Emuladores' '&' 'Roms/MS-D.O.S/Aladdin/Iniciar'`"
    

    然后没有在你的函数中正确设置$1

    总结一下:Use more quotes.

    【讨论】:

    • 我解决了它,它用于制作的 .desktop 文件中的“PATH”变量。我删除了那条线,现在它可以工作了
    猜你喜欢
    • 2019-06-22
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    相关资源
    最近更新 更多