Bash脚本用于在路径中查找脚本并启动另一个脚本

时间:2014-09-24 01:31:37

标签: linux bash shell sh zsh

看,我有这个脚本,它用于查找,一个脚本在“路径”中,并启动另一个名为“.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

运行

感谢您的帮助。

1 个答案:

答案 0 :(得分: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