Valhalla Legends Forums Archive | General Programming | Shell scripting

AuthorMessageTime
Archonist
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.

[code]
#!/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: krazedx86@gmail.com
#AIM: krazedx86
[/code]

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

[code]
#!/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: krazedx86@gmail.com AIM: krazedx86
[/code]

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

[code]
#!/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: krazedx86@gmail.com AIM: krazedx86
[/code]

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

[code]
#!/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: krazedx86@gmail.com AIM: krazedx86
[/code]
May 23, 2005, 4:07 AM
UserLoser.
Good job
May 23, 2005, 4:12 AM
mynameistmp
Hello krazed. A tip:

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

The same thing could be achieved with:

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

May 24, 2005, 6:42 AM
Newby
You should start playing around with variables and "read" to see what you can do. :)
May 24, 2005, 2:40 PM
Archonist
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.  :)
May 24, 2005, 4:30 PM
mynameistmp
Keep posting. If you come up with anything interesting I'll show you some useful ones I've got.
May 25, 2005, 7:10 AM
Archonist
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.
[/quote]
May 25, 2005, 7:29 PM
mynameistmp
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.
May 26, 2005, 6:58 AM

Search