mtWeb  Home > Linux > General purpose shell scriptsSitemap  Search

General purpose shell scripts

Posted by martin on 1 Oct 2001, last updated on 1 May 2002.

Another reason to get rid of rpmdrake (if you have Mandrake installed with it), besides the fact that the version that comes with Mandrake 8.1 is very much buggy is this simple script to check on which cd a package is, you'll have to first create the text files of course like /mnt/cdrom/Mandrake/RPMS/# ls > /usr/local/share/mdk_cd1.txt

grep=/bin/grep
printf "\nDisc 1\n"
$grep -i $1 /usr/local/share/mdk_cd1.txt
printf "\nDisc 2\n"
$grep -i $1 /usr/local/share/mdk_cd2.txt
printf "\nDisc 3\n"
$grep -i $1 /usr/local/share/mdk_cd3.txt

No shell built-ins used so no sha bang necessary. You might wanna use something like "^$1" instead of just $1 to find only packages that begin with the specified parameter. Also remember to remove the extensions from the filenames list if you didn't do the previous one (yesterday I wanted to install CGI.pm and I first searched for CGI and then for pm and all 3 lists flashed before my eyes).

A script that will save disk usage statistics for your system (only directories larger than 10MB are reported but it can be easily tweaked for something else) in XML format.

#!/bin/bash
grep="/bin/grep"
output="du_stats.xml"

TMPFILE=`/bin/mktemp -q /tmp/$0.XXXXXX`
if [ $? -ne 0 ]; then
	echo "$0: Can't create temp file, exiting..."
	exit 1
fi

/usr/bin/du -mx / | $grep -E "^([0-9]){2,}" >> $TMPFILE
/usr/bin/du -m /usr | $grep -E "^([0-9]){2,}" >> $TMPFILE

txt=`/bin/cat $tmpfile`
/bin/rm -f $tmpfile

echo "<stats>" >> $output
tmp=`/bin/date +"%e %b %Y"`
echo "<date>$tmp</date>" >> $output
j=1
for i in `echo $txt`
do
	let rem=j%2

	if [ $rem = 1 ]
	then
		echo "<dir size=\"$i\">" >> $output
	else
		echo "$i</dir>" >> $output
	fi
	let j++
done

echo "</stats>" >> $output

If you have noticed there are two invocations of du this is because I have / and /usr on separate partitions, and I used -x the first time to prevent du reporting usage on other filesystems other than the root.

The Wget scripts have been moved to a page dedicated to Wget.

Comments

Help me write (or just write it!) this script.
by Deborah (williamsde@usa.redcross.org) on 5 Nov 2002 1:26am GMT

Create a list of all users on your server. The file/ etc/passwd contains a list of all users, but unfortunately many system processes are also listed as users. Compose a script that will take the /etc/paswd file and extract only real users to a file named LinuxUsers.

The /etc/passwd file contains a record of each line, organized in the following fields:

<login name>:<password (shown as x)>:<user id>:<group id>:<full name>:<home directory>:<default shell>

Please explain.

Also supply the code for the <Process$line> section and all of the code outside the loop that will achieve the desired results.

All users on a system
by martin on 5 Nov 2002 9:05am GMT

Usually the user id's of real user are above 500 or 1000. You can use sed (and probably awk, I'm not really into using awk) to extract the usernames then - there is example code in the page about WGet which shows how to process a text file line by line.

Grep with variables in shell script
by david (david@vio.com) on 7 Nov 2002 1:21pm GMT

I want to copy all that has example: "Nov 6 12:" from /var/log/messages and redirect it into new.txt

If I run these commands from the command line all works ok but this does not work from a shell script (bash). Seems like the Nov, 6 and 12: are all seen as different items from the script.

DATE=`date | cut -c5-14`

grep $DATE /var/log/messages >> /new.txt

Quote it
by martin on 9 Nov 2002 12:02pm GMT

Just quote the date, it has spaces in it ;-)

Script?
by Sean (VieRus_8u@hotmail.com) on 27 Feb 2003 8:22am GMT

I need a Linux script that prompts the user for their name, and then displays a greeting which includes the date. It should look like this when you run it,

What is your name?

Sean

Hello, Sean. Today is March 7, 2003

Hope you can help me out Im having a heck of time trying to do it.

Thanx

Re: Script?
by Caspah DeGhost (deghost1@frontiernet.net) on 31 Mar 2003 4:21am GMT

#!/bin/bash

echo "What is your name?"

read name

echo "Hello, $name. Today is" ; exec date

-----

thats a very quick thingy that might help .. its output is like this....

----

[Caspah@DeGhost]# ./sean-script.sh

What is your name?

Caspah

Hello, Caspah. Today is

Sun Mar 30 23:11:03 EST 2003

[Caspah@DeGhost]#

----

you might be able to use $LOGNAME

to get the user name ... but an account name over an actual persons name isnt all that personal :)

Re:Help me write (or just write it!) this script.
by Caspah DeGhost (deghost1@frontiernet.net) on 31 Mar 2003 4:27am GMT

would it be easyer for you if you just did a ls on the /home directory ?

lame script
by Roesingape (roesingape@roesingape.org) on 24 May 2003 3:44pm GMT

I wanna write a script where if you drag and drop a wav file onto an icon, it converts it to mp3 then deposits it in a default directory...where should I start? this will be my first script, thanks.

logfile help
by Throckmorton (throckmorton@gmx.li) on 4 Jul 2003 10:44am GMT

Hello,

I am trying to write a script that will automatically at the end of the month go through /var/log and combine all the logfiles from the same month into one file. The logs are always keyed with the date. Examples: mail-20030325.gz or access.log-20030510.gz

My problem is just figuring out what the current dd-mm-yyyy is and then telling the linux box to decompress and cat the month before. naturally from cron ;)

help?

Need help
by Todd () on 25 Sep 2003 1:31am GMT

I am trying to write a bash script that will parse through the /etc/passwd file and print it out by group. All members of the same group need to be output together. Can i get some help?

socket in C# or Java
by Ahmet Saygin (asaygin@hotmail.com) on 26 Sep 2003 9:22am GMT

I need to write a socket in C or java. Despite php is very C like, I didn;t understand what does what.

Can someone help me please?

Linux scripting for local users.
by Tywanna (atywanna@yahoo.com) on 17 Oct 2003 7:35pm GMT

I need a script to create a student account. I have created a list of all users on the server, but I'm not sure where to go from there.

Linux scripting for local users.
by Tywanna (atywanna@yahoo.com) on 18 Oct 2003 6:17pm GMT

I need a script to create a student account. I have created a list of all users on the server, but I'm not sure where to go from there.

I'll help
by paul (paul@youza.com) on 3 Jan 2004 11:11pm GMT

If anyone needs help with shell scripts drop me a line at paul@youza.com and I'll do some for you :) Thanks (No charge)

a very simple question
by jk_ramirez on 1 Mar 2004 9:51pm GMT

hi

I'm a beginner and I want to write a acript that shows a message an then it logs out automatically, if you can help me out please send me a message to jk_ramirez@yahoo.com

thanx

scripts
by alex (kisakye@myself.com) on 5 Apr 2004 7:33pm GMT

can anybody help me write a script than will mount a floppy move "mv" all contents to a folder on the machine and successfully umount the floppy?

thanks

shell script for move file
by loius (loius_757@hotmail.com) on 7 Jul 2004 9:14am GMT

From loius:

can u answer me faster??

Question:write a shell script that replaces all ".html" file names with ".html.old" in the current directory?

RE:shell script for move file
by Girish (phygiri@yahoo.com) on 19 Jul 2004 8:44am GMT

Loius, use the simple command

rename .html .html.old *.html

Scripts for unjaring java files
by gtay (sargaras@hotmail.com) on 11 Aug 2004 10:46am GMT

Question: write a shell script that looks into a directory finds all the .jar files, unjars and moves them to another directory then compiles the java files.

Linux Scripting for moving files
by Jeevan (jeevanchanda@itnation.com) on 2 Sep 2004 9:18am GMT

I want to move all the files from one folder to another folder.

to learn abt shell scripts
by glad (glad2cu_2@yahoo.com) on 26 Sep 2004 1:09pm GMT

help me to lean to edit shells and iam a newbie,so i need help regarding scripts

convert .png file to .eps
by Jyoti (jyoti.agrawal@inria.fr) on 14 Oct 2004 1:35pm GMT

I have 40 .png files I want to convert all of them in .eps files. Is there any way to convert them at a time; If I do it one by one it will take long time.

Thanks

c++, java, linux, web-designing etc.
by gautam joshi (gautam_joshi_it@yahoo.co.in) on 27 Oct 2004 11:32am GMT

i like to know about new in computer i like to take every tutour about computer. i like animation programming etc.

shell scripts
by masiur (masiur1981bd@yahoo.com) on 23 Nov 2004 6:08am GMT

hello help me to do this,my topics is "displays its three command line arguments on separate lines.An error message should be displayed and the script terminate if the script is supplied with less than or more than three arguments.

so i need help regarding scripts.

thanks

pls help!
by karen (karenm@rediffmail.com) on 9 Dec 2004 8:10am GMT

i want to execute a java program using shell script in suse linux.

my file is at a location.for eg:

cd /opt/jakarta/tomcat/karen(on the Konsole we tyoe this & then type)

java EncoderProxy to execute it.

How do we do that using shell script?

how to find file size using shell script
by gopal (gopal.krishnan@philips.com) on 14 Dec 2004 10:55am GMT

In the server there are many users directory.how to find file size for each user directory and if the size is greater than specifc value that to be displayed.how to do using shell script or perl script.

Payroll script
by sameer (sameer_khanna@yahoo.com) on 22 Dec 2004 6:00am GMT

Hi,

kindly provide me a shell scipt which picks emplyee id, name, basic salary from text based database, fields separated by delimiters and calculate the final

slaray=basic + HRA + CCA + TA - IT

where HRA = 20% of basic salary

and IT =10% of basic salary

CCA

200 if salary less than 5000

500 if salary between 5-10,000

1000 if salary between 10-20,000

TA

450 if salary less than 5000

750 if salary between 5-10,000

1200 if salary between 10-20,000

Kindly pride me the solution asap, i will be grate gul as this need for my exam.

Help me
by brad (cyrad2@yahoo.com) on 24 Jan 2005 12:30am GMT

I need a quick bash script that can cook toast, clean the sink, and start my shower every morning. Here is what i have so far:

cd /toaster