Bash script works in Debian not in OpenSuse


Results 1 to 10 of 10

Thread: Bash script works in Debian not in OpenSuse

  1. #1
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494

    Bash script works in Debian not in OpenSuse

    I have written a few bash-scripts to automate the installation of various applications from source.
    It works fine in Debian Lenny with bash version 3.2 (GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu) )
    In OpenSuse 11.4 with bash version 4.1 (GNU bash, version 4.1.10(1)-release (x86_64-suse-linux-gnu) ) it doesn't work.

    Details:
    All variables are stored in one file "variabler", sourced by installation scripts.
    The start script uses command "dialog" for easy selection, then simply calls appropriate installation script. There is one separate file for each application.

    The dialog is displayed, I can select & deselect - but once I hit <enter> script simply stops. There is no output at all, no error no nothing.
    However, I can run each installation script directly so obviously problem is that the install scripts are not called correctly in Suse.
    The scripts are all in /usr/local/src/ which is in $PATH
    I have tried calling the install scripts with full path as well, no difference.
    All variabes are correct - remember I can succesfully install everything by executing the install scripts one by one.

    List of files:
    cert_install - start script
    variabler - contains all variables such as application version, where source is etc
    inst_apa, inst_openssl etc - executable scripts that installs one application each.

    The start script:
    Code:
    #!/bin/bash
    # Kontrollera att filen 'variabler' finns, avsluta annars
    if [ ! -f variabler ]; then
    	echo "Filen 'variabler' saknas"
    	exit
    fi
    # Läs in variablerna
    source variabler
    # Skapa logg-katalog om den inte finns
    if [ -f logs ]; then
    	echo "Kan inte skapa katalog 'logs' - finns en fil med det namnet."
    	echo "Radera den först, eller kör vidare utan att loggar sparas."
    fi
    if [ ! -d logs ]; then
    	mkdir logs     <---------- This works fine!
    	export LOGGDIR=logs
    fi
    
    # Val av program att installera
    # Kommandot 'dialog': sista 3 värdena är Totalhöjd Totalbredd Boxhöjd
    cmd=(dialog --separate-output --checklist "Val av program att installera: \n 
    	OBS! Openssl & PCRE are required for Apache, Php & Modsec" 18 76 7)
    	options=(1 "Openssl" on
    			2 "PCRE" on
    			3 "Apache" off
    			4 "MySQL" off
    			5 "Php" off
    			6 "ModSecurity - experimental!" off
    			7 "ModSec Core Ruleset - experimental!" off)
    
    	choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
    	clear
    	for choice in $choices
    	do
    		case $choice in
    			1)
    				echo "Installerar OpenSSL"
    				inst_openssl 2> >(tee $LOGGDIR/openssl_error)
    				;;
    			2)
    				echo "Installerar PCRE"
    				inst_pcre 2> >(tee $LOGGDIR/pcre_error)
    				;;
    			3)
    				# Kontrollera beroenden (pcre & openssl)
    				echo "Installerar Apache"
    				inst_apa 2> >(tee $LOGGDIR/apache_error)
    				;;
    			4)
    				echo "Installerar MySQL"
    				inst_mysql 2> >(tee $LOGGDIR/mysql_error)
    				;;
    			5)
    				echo "Installerar Php"
    				inst_php 2> >(tee $LOGGDIR/php_error)
    				;;
    			6)
    				echo "Installerar ModSecurity"
    				inst_modsec 2> >(tee $LOGGDIR/modsec_error)
    				;;
    			7)
    				echo "Installerar ModSecurity Core Ruleset"
    				inst_crs 2> >(tee $LOGGDIR/crs_error)
    				;;
    		esac
    	done
    Example installation script - "inst_openssl":
    Code:
    #!/bin/bash
    # Skapat 2011-11-01
    # Detta script kompilerar Openssl från källkod
    
    # Variabler för installationsscriptet läses in från filen "variabler"
    source variabler
    
    # Ladda hem & packa upp källkoden
    cd $SRC_DIR
    wget $REPO_OPTIONS $REPO_PROTOKOLL/$REPO/openssl-$OPENSSL_APPVERSION.tar.gz
    tar -zxf openssl-$OPENSSL_APPVERSION.tar.gz
    cd openssl-$OPENSSL_APPVERSION
    ./config --prefix=/nih/openssl-$OPENSSL_APPVERSION/ --openssldir=/od/openssl-$OPENSSL_APPVERSION 
    make && make install
    
    # Ta bort gamla bin-filen, länka in nya
    KAT=/usr/bin/
    IDAG=`date +%d%m%y`
    if [ -h $KAT/openssl ] ; then
    	unlink $KAT/openssl
    else
    	if [ -f $KAT/openssl ] ; then
    		mv $KAT/openssl $KAT/openssl.$IDAG
    		ln -s /nih/openssl-$OPENSSL_APPVERSION/openssl $KAT/openssl
    	fi
    fi
    My first guess is there is a difference between bash 3.2 & 4.1 that causes the problem.
    I'm currently investigating this line in cert_install :
    choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

    Any ideas?
    In pingvino veritas!

  2. #2
    Join Date
    Jul 2003
    Location
    Spokane, Washington
    Posts
    580
    Is the screen clear when the script stops?
    Last edited by Pafnoutios; 11-02-2011 at 01:14 PM.

  3. #3
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494
    Yes, that's a very good idea - in fact, I often use it myself.
    I should have added why I'm sure it's in main script:
    As you can see the directory "logs" is created if it doesn't exist, but the echo command preceding each call to install script is not executed. (echo "Installerar ..." )
    However, maybe I should add a few echo before & after the lines "choices=..." and for loop.
    I'll do that tomorrow when I'm back at office.
    In pingvino veritas!

  4. #4
    Join Date
    Jul 2003
    Location
    Spokane, Washington
    Posts
    580
    Oops. You were fast with that. I had decided very shortly after posting my suggestion that you probably had enough debugging echo statements and editted it.

  5. #5
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494
    Hehe, you know what?
    Before reading your post I, too, thought I had enough debugging echo statements, but while answering I realized I didn't!
    So you were right even though you thought you were wrong, and I was wrong even though I thought I was right!

    Now I added a few "echo", here's what happens:
    First the modified part of master script
    Code:
    			7 "ModSec Core Ruleset - experimental!" off)
    	echo "Before choices"
    	choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
    	clear
    	echo "After choices"
    	for choice in $choices
    	do
    		echo $choices
    		echo $choice
    		echo "After for"
    		case $choice in
    			1) echo "Choice 1"
    				echo "Installerar OpenSSL"
    ..... ( rest of script)
    		esac
    		echo "After esac"
    	done
    And here's the result having chosen 2 apps to install:
    Code:
     After choises
    "1" "2"
    "1"
    After for
    After esac
    "1" "2"
    "2"
    After for
    After esac
    So the for-loop runs correctly, the variable $choice is set correctly - but there is no hit for my choices!?
    Interresting, but leaving me even more puzzled!
    In pingvino veritas!

  6. #6
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494
    Aha, getting closer!
    I added a line
    choice=1
    just before the "case $choice..." line - and alas, it worked!
    Checking the output earlier, variable $choice is set to "1" including the doublequotes!
    So I need to find a way to set the variable to numericals only, without any quotes.
    In pingvino veritas!

  7. #7
    Join Date
    Sep 1999
    Location
    Cambridge, UK
    Posts
    509
    The path of least resistance may be to eat the quotes later:
    Code:
    case ${choice//\"/} in
    ...

  8. #8
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494
    Thanks furrycat, that did it!

    ... Just wonder, could you please explain to me why that works?
    Using your code I can see that the last quote is removed, but the first one is too and I can't see how.
    In pingvino veritas!

  9. #9
    Join Date
    Sep 1999
    Location
    Cambridge, UK
    Posts
    509
    Two slashes means multiple replacements. One slash would just replace the first occurrence.

  10. #10
    Join Date
    Sep 2003
    Location
    Rochester, MN
    Posts
    3,604
    Variable types in bash are tricky. There's a document about them here: http://tldp.org/LDP/abs/html/untyped.html

    Honestly, I find that even that doesn't come close to covering all of the possible gotchas with them though. This is why I'm a fan of statically typed languages.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •