簡體   English   中英

Bash腳本,用於在路徑中查找腳本並啟動另一個腳本

[英]Bash Script for find a Script in a path and launch that another script

看,我有這個腳本,它是供查找的,是“路徑”中的一個腳本,然后啟動另一個名為“ .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-DOS / 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-DOS / 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上運行

感謝您的任何幫助。

如果帶空格的路徑引起問題,那總是意味着您沒有使用足夠的引號。

具體來說,您正在使用的變量可以包含空格而無需“引用”它們。

例如下面的行。

RUTA_DEL_SOFTWARE="`Encontrar_Ruta $0`"

您不會引用$0 因此,當您運行sh "/media/Compartido/Juegos/Emuladores & Roms/MS-DOS/Aladdin"/Iniciar盡管在外殼程序看到的行上方使用該路徑(如$0 )時將路徑引用為參數,是:

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

這樣就不會在函數中正確設置$1

總結一下: 使用更多引號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM