Find the answer to your Linux question:
Results 1 to 5 of 5
This is my first working bash script. I got tired of typing 'apt-cache search' and 'apt-get install' so I made this: Code: #! /bin/bash things=`apt-cache search $1` dline=0 for thing ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    34

    Is this useful?

    This is my first working bash script. I got tired of typing 'apt-cache search' and 'apt-get install' so I made this:
    Code:
    #! /bin/bash
    
    things=`apt-cache search $1`
    dline=0
    for thing in $things
    do
    	dline=$(( $dline + 1 ))
    	gpkg[$dline]=$thing
    done
    
    line=0
    pkg=0
    while [ $line -ne $dline ]
    do
    	if [ "${gpkg[$line + 1]}" = "-" ]
    	then
    		pkg=$(( $pkg + 1))
    		tpkg[$pkg]=${gpkg[$line]}
    		echo "$pkg ${gpkg[$line]} -"
    	else
    		if [ "${gpkg[$line + 2]}" = "-" ]
    		then
    			echo 
    		else
    			echo -n " ${gpkg[$line + 1]}"
    		fi
    	fi
    	line=$(( $line + 1 ))
    done
    echo
    echo -n "choose a file number to install: "
    read linedir
    echo ${tpkg[$linedir]}
    apt-get install ${tpkg[$linedir]}
    Plus I had a lot of free time on my hands. I think it will speed up apt-get installations immensely. Go ahead and try it out.

  2. #2
    Just Joined!
    Join Date
    Mar 2008
    Posts
    34
    I called it getpkg
    Heres how to use it, I forgot to put this in. You have to chmod a+x it first, then when you run it, specify a package that you want to install. ex
    Code:
    #  chmod a+x getpkg
    #  ./getpkg gettext
    I programmed it for ubuntu, it may not work for other distros

  3. #3
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    That looks pretty interesting, but I see you have no handling for root/sudo access. I know a lot of current thinking is that only what needs to be run as root should be.

    Also I see that you are using a root prompt. Bear in mind that the root user is disabled by default in Ubuntu. While I personally didn't like this behaviour at the start I do think it is a sensible approach given the userbase of Ubuntu is largely new users so I usually discourage users from setting a root password. With all that in mind I'd prefix the last line with sudo as it is an interactive script and a password can be provided. Also I would add exit to provide a clean exit to the script. In this case you might want to use the exit status from apt-get rather than declare it to be a specific value.

    The last two lines:
    Code:
    sudo apt-get install ${tpkg[$linedir]}
    exit $?

  4. #4
    Just Joined!
    Join Date
    Mar 2008
    Posts
    34
    Whats the $? at the end for?

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    "$?" is the exit status of the last command. In this case, it will be the exit status of the "apt-get install ...". This way, your script will exit with the same status as apt, so if apt errors, your script will report an error.

    One other note:

    Whenever you use a variable in Bash scripting, you should quote it. Suppose I call your script like so:
    Code:
    ./getpkg "hello world"
    Now, in your first line, the following gets executed:
    Code:
    things=`apt-cache search hello world`
    Notice how what I passed as one argument ("hello world") has now become two ("hello" and "world"). This will be avoided if you quote the "$1".


    Remember also that "useful" is very subjective. If it's useful to you, then it's useful! I have a bunch of scripts and small C programs lying around my box that I've never shown to anybody, but which are useful to me, and were therefore worth the time to write.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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