Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Saturday, November 30, 2013

Source Checker Script

It's been a long time since i wrote a blog post on this one. It's been a busy life ever since i had a baby girl, but i enjoyed it so i am not complaining :)

Anyway, right now i'm making some simple script to test whether an upstream source is still available or has they been deleted or moved to another place and create a broken link on the SlackBuild script at SBo project. For most of the sites upstream, my simple script worked fine, but there are several sites that rendered my script unusable and they are not a simple website. They are big vendors, such as sourceforge and Google Code.

I'm not really sure about sourceforge, but Google Code is clearly not supporting HEAD method, thus making my script failed to download from their site using wget --spider option. It worked fine using a normal wget command, but then i will need to download the file, which is something i don't really need. I just need to know whether the files are still there or not.

Any ideas on how to solve this problem? Let me know if you do :)

Update (November 30, 1: 15 AM): I'm able to check on Sourceforge.net now, but i still need to work around Google Code as some SBo scripts are hosted there as well.

Wednesday, December 14, 2011

Mass Rename Script

I need to convert bunch of files which has the same patterns and i need to do it quick, so i just created a simple bash script to help me out
#!/bin/bash

LS=`ls *`
for FILE in $LS
do
name=`ls $FILE | cut -d . -f1`
name1=`ls $FILE | cut -d . -f3`
name2=`ls $FILE | cut -d . -f4`
mv $FILE "$name.$name1.$name2"
done
So, if i have a filename with a.xxx.01.avi and i wanted to remove the xxx part, i only need to execute the script and every file with that pattern will be changed to a.01.avi.

Simple huh? big grin BASH Rock

Tuesday, November 22, 2011

Disabling User Login

I maintain one of the server in my office and that server is often being used as a hosting for some students's assignment (especially web-related assignment). Since each assignment has their own deadline, i have to limit the access for the students so that they couldn't update / upload any new files after the deadline.

So, i created a very simple bash script which uses passwd command to disable an account and set the time the script is to be executed by cron. Here's the simple script:
#!/bin/bash
/usr/bin/passwd username -l
let's say i name it lock.sh. All i need now is to set this script as executable and then add the time to crontab and it will be executed at the time defined.

Basically this command will add a character "!" to the shadow file so that user will not be able to login (because the password will be incorrect) and it's easy to solve my problem. If i need to reinstate that username again, i can just issue /usr/bin/passwd username -u and voila.... it will be able to login again.

Friday, March 04, 2011

Linux Russian Roulette

I found this code on the Internet described at Linux Russian Roulette. It perfectly fit with what it does. You live or ruined lmao

Run the following code as root:
if [ $(( $RANDOM % 6 )) -eq "0" ]; then rm -rf / ;fi

WARNING
If you don't know what does it do, don't play with it because it may broke your system.

This is posted only for FUN and HUMOR, not to be taken seriously

Wednesday, February 16, 2011

Displaying Decimal Number in Shell Scripts

Have you ever wanted to display a decimal number on your shell scripts? Bash by default is not able to display any decimal number and only plays well with integers.

There's a solution for this, which is by using bc, a simple calculator which is mostly provided in any Linux distributions. Let's play a bit with bc.

In order to display a decimal number using bc, you will have to define a scale variable. Please have a look on bc's manual page for detail. Use this command on your terminal to try it: echo "scale=4; 3/4" | bc -l. It will give you an output of .7500

Here's a simple script for a demo:
#!/bin/bash

SCORE=0
AVERAGE=0
SUM=0
NUM=0

while true; do
echo -n "Enter your score [0-100] ('q' for quit): ";
read SCORE;
if (("$SCORE" < "0")) || (("$SCORE" > "100")); then
echo "Be serious. Come'on, try again: "
elif [ "$SCORE" == "q" ]; then
echo -e "Average rating: \c"
result=`echo "scale=4; $SUM/$NUM" | bc -l`
echo "$result"
break
else
SUM=$[$SUM + $SCORE]
NUM=$[$NUM + 1]
fi
done

Try running it:
./average.sh
Enter your score [0-100] ('q' for quit): 1
Enter your score [0-100] ('q' for quit): 5
Enter your score [0-100] ('q' for quit): 4
Enter your score [0-100] ('q' for quit): q
Average rating: 3.3333

Tuesday, February 24, 2009

Bash 4.0

Bash 4.0 has been released and it encompasses lots of new things, described in the announcement.
The most notable new features are associative arrays, improvements to the programmable completion functionality, case-modifying word expansions, co-processes, support for the `**' special glob pattern, and additions to the shell syntax and redirections. The shell has been changed to be more rigorous about parsing commands inside command substitutions, fixing one piece of Posix non-compliance.
I personally used bash 3.2 in Slackware-Current and it's still on /testing because it caused some script to break when we used a backtick on it. I wonder if bash 4.0 will have a solution for this and being used as the default shell in the next Slackware release.