Valhalla Legends Archive

Programming => General Programming => Topic started by: Archonist on May 22, 2005, 11:07 PM

Title: Shell scripting
Post by: Archonist on May 22, 2005, 11:07 PM
I've recently, (today) gotten into shell scripting. I've written four scripts today, I'll post them here and let you guys discuss them.

We'll start with helloworld.sh.


#!/bin/bash

echo "Hello world bash script!"
echo "Writen by krazed of x86!"
echo "The first of many. :P"

#this script is pointless, shush.
#what directory are we in!?

pwd

#what's in this directory!?

ls

#and, what's the uptime on this boxs?

uptime

#how about the processes being ran?

ps -aux

#Okay, that's all for today, 'folks.
#Written by krazed of x86.
#E-Mail: [email protected]
#AIM: krazedx86


This is called cmdlisting.sh. It takes the contents of the command directorys and lists them into four different .txt files.


#!/bin/bash

echo "Command listing bash script."
echo "Purpose: Copy your command lists to three different text files."

# First, we start off with /usr/bin, the default commands.

ls /usr/bin > defaultcommands.txt
echo "Contents of /usr/bin written to defaultcommands.txt"
echo " "

# Okay, now we have copied the contents of /usr/bin into the file defaultcommands.txt, Next!
# Now, time for /bin, the system commands.

ls /bin > systemcommands.txt
echo "Contents of /bin written to systemcommands.txt"
echo " "

# Okay, contents of /bin are now in systemcommands.txt
# Let's move on to /usr/X11/bin, the X windows programs.

ls /usr/X11/bin > xwincommands.txt
echo "Contents of /usr/X11/bin written to xwincommands.txt"
echo " "

# Alright, last but not least, let's list your installed programs. /usr/local/bin

ls /usr/local/bin > installedprograms.txt
echo "Contents of /usr/local/bin written to installedprograms.txt"
echo " "
echo "Script executed, no errors, not bad for my second script ever, eh?"

# Okay, that's the script for yah.
# It's not too bad for my second script ever.
# This script written by krazed of the x86 crew.
# We're on IRC at irc.tehnetwork.org:6667 #x86.
# E-Mail: [email protected] AIM: krazedx86


And, this is the third one. This one is actually pretty nifty, it's me experimenting with ANSI color codes, and scripting in color.


#!/bin/bash

# A small shell script written so I could fuck around
# and get the hang of coding with colors in shell scripts.

# A full list of all the ANSI color codes.
#1m     -     Change text to hicolour (bold) mode
#4m     -        "    "   "  Underline (doesn't seem to work)
#5m     -        "    "   "  BLINK!!
#8m     -        "    "   "  Hidden (same colour as bg)
#30m    -        "    "   "  Black
#31m    -        "    "   "  Red
#32m    -        "    "   "  Green
#33m    -        "    "   "  Yellow
#34m    -        "    "   "  Blue
#35m    -        "    "   "  Magenta
#36m    -        "    "   "  Cyan
#37m    -        "    "   "  White
#40m    -     Change Background to Black
#41m    -        "       "      "  Red
#42m    -        "       "      "  Green
#43m    -        "       "      "  Yellow
#44m    -        "       "      "  Blue
#45m    -        "       "      "  Magenta
#46m    -        "       "      "  Cyan
#47m    -        "       "      "  White
#49m    -        "       "      "  Default background
#7m     -     Change to Black text on a White bg
#0m     -     Turn off all attributes.

echo -e "\x1B[34;49m This is blue text on default background."
echo -e "\x1B[32;49m This is green text on default background."
echo -e "\x1B[37;42m This is white text on green background."

# This script written by krazed of the x86 crew.
# We're on IRC at irc.tehnetwork.org:6667 #x86.
# E-Mail: [email protected] AIM: krazedx86


I re-wrote cmdlisting.sh so I could practice getting color codes down. So here it is, the new and improved, cmdlistingv2.


#!/bin/bash

echo -e "\x1B[34;49m" "Command listing bash script, version2. Rewritten 05/22/05."
echo -e "\x1B[34;49m" "Purpose: Copy your command lists to four different text files."
echo " "

# First, we start off with /usr/bin, the default commands.

ls /usr/bin > defaultcommands.txt
echo -e "\x1B[31;49m" "Contents of /usr/bin written to defaultcommands.txt"
echo " "

# Okay, now we have copied the contents of /usr/bin into the file defaultcommands.txt, Next!
# Now, time for /bin, the system commands.

ls /bin > systemcommands.txt
echo -e "\x1B[31;49m" "Contents of /bin written to systemcommands.txt"
echo " "

# Okay, contents of /bin are now in systemcommands.txt
# Let's move on to /usr/X11/bin, the X windows programs.

ls /usr/X11/bin > xwincommands.txt
echo -e "\x1B[31;49m" "Contents of /usr/X11/bin written to xwincommands.txt"
echo " "

# Alright, last but not least, let's list your installed programs. /usr/local/bin

ls /usr/local/bin > installedprograms.txt
echo -e "\x1B[31;49m" "Contents of /usr/local/bin written to installedprograms.txt"
echo " "
echo -e "\x1B[34;49m" "Script executed."
echo -e "\x1B[0m"

# Okay, that's the script for yah.
# This script written by krazed of the x86 crew.
# Script rewritten so I could fuck with color codes.
# We're on IRC at irc.tehnetwork.org:6667 #x86.
# E-Mail: [email protected] AIM: krazedx86
Title: Re: Shell scripting
Post by: UserLoser. on May 22, 2005, 11:12 PM
Good job
Title: Re: Shell scripting
Post by: mynameistmp on May 24, 2005, 01:42 AM
Hello krazed. A tip:


echo "Contents of /usr/bin written to defaultcommands.txt"
echo " "


The same thing could be achieved with:


echo -e "Contents of /usr/bin written to defaultcommands.txt\n"


Title: Re: Shell scripting
Post by: Newby on May 24, 2005, 09:40 AM
You should start playing around with variables and "read" to see what you can do. :)
Title: Re: Shell scripting
Post by: Archonist on May 24, 2005, 11:30 AM
Hiya tmp, and alright thanks. I figured it was something like that, but.. well I actually don't know why I didn't try it.. Oh well, and okay newby I'll do that.  :)
Title: Re: Shell scripting
Post by: mynameistmp on May 25, 2005, 02:10 AM
Keep posting. If you come up with anything interesting I'll show you some useful ones I've got.
Title: Re: Shell scripting
Post by: Archonist on May 25, 2005, 02:29 PM
The only other one I've written so far was one to load a zDSBot off of my server, because some bnet kids we're too imcompetent to be able to type "cd zds", "screen ./zDSBot3."

It was along the lines of, uhm..

Quote
#!/bin/bash

# Purpose: Load a zDSBot off the server.

cd zds && screen ./zDSBot3

# ({CTRL + A} D) to close the screen, screen -r to re-open it.

# Written by krazed of the x86 crew.
Title: Re: Shell scripting
Post by: mynameistmp on May 26, 2005, 01:58 AM
Challenge:

Using bash, make it so that when you boot into X Windows with your default user (krazed or whichever login you use) it always starts up a terminal (of your choice) that is running with UID 0 (root). This is assuming that your default user is not UID 0.

I used to have it so that whenever I booted X, xfterm4 popped up already tailing my logfiles (which required privs), so I've had to do this before. Let's see how similar our methodology is.

Let me know if you need any clarification/hints.