×
Create a new article
Write your page title here:
We currently have 3,189 articles on s23. Type your article name above or create one of the articles listed here!



    s23
    3,189Articles

    Bash Scripts: Difference between revisions

    Content added Content deleted
    imported>mutante
    imported>DrOwl
    (added KEDada)
    Line 124: Line 124:
    echo "memory usage: $percent %"
    echo "memory usage: $percent %"
    </pre>
    </pre>


    === Auto Archive of CD and DVD's ===
    A simple-ish "bash" script to copy "archive" cd/dvd's of to a local disk,
    I used to copy old disk based backups to my new NAS box, it isnt quite right and should have more options instead of static links to devises etc but =)
    Its also my first 23 lines of code, code

    "Keds Auto Disk Archiver":

    <pre>
    KEDADAstarted="none yet"; KEDADAstoped=""; KEDADAcount="0" ; KEDADAtodo="0"; ORIG_IFS=$IFS ; IFS=$'\n'
    while : ; do
    if [ `cat /etc/mtab | grep /dev/hdc |wc -l` != 0 ] ; then
    clear
    KEDADAcount=0 ; KEDADAstart="Started archive of cdrom0: `date`" ; KEDADAtodoToT=`ls /media/cdrom0/ | wc -l `
    time for x in `ls -q /media/cdrom0/` ; do
    clear
    echo -e "$KEDADAstart \n copying \"$x\" \n ( $KEDADAtodo / $KEDADAtodoToT ) \n"
    time cp -r /media/cdrom0/$x /mnt/mu/KEDADA/
    KEDADAtodo=$((KEDADAtodo+1))
    done
    KEDADAstoped="Compleat: `date`"
    echo -e "$KEDADAstoped"
    umount /media/cdrom0/
    sleep 23
    eject -v cdrom
    else KEDADAcount=$((KEDADAcount+23))
    clear
    echo -e "Last Archive: $KEDADAstarted \n $KEDADAstoped\n Now in sleeping period $KEDADAcount. "
    sleep 23
    fi
    IFS=$ORIG_IFS
    done
    <pre>



    === Don't do this at home, kids: ===
    === Don't do this at home, kids: ===
    Line 145: Line 180:
    This will completely corrupt an entire hard disk:
    This will completely corrupt an entire hard disk:
    dd if=/dev/dsp of=/dev/hda
    dd if=/dev/dsp of=/dev/hda










    Revision as of 13:46, 24 March 2009

    Question from Pastebin

    #!/bin/bash
    
    IPS=$(ifconfig | awk '/inet/ { print $2 }' | awk -F ":" '{ print $2 }')
    
    NUMIPS=$(echo $IPS | wc -w)
    
    COUNT=1
    for i in $IPS
    do
      export IP$COUNT=$i
      COUNT=`expr $COUNT + 1`
    done
    
    # -----------------
    # I need to display all ips like this to user:
    #
    # 1. 123.123.123.123
    # 2. 22.22.22.22
    # 3. 32.32.32.222
    #
    # Please choose which IP address to use:
    # Prompt user here
    
    
    

    < planetxm> on Efnet #debian 02:33, 25 Feb 2005 (CET)

    Answer

    #!/bin/bash
    
    # IPS=$(ifconfig | awk '/inet/ { print $2 }' | awk -F ":" '{ print $2 }')
    IPS="192.168.23.5 47.45.56.11 189.67.24.21"
    count=1
    
    for IP in $IPS;
    do
    IP[$count]=$IP;
    echo "Found IP $count - ${IP[count]}";
    count=`echo $count+1 | bc`
    done
    
    
    read -p "Which IP do you want to use? (number)" choice
    
    IPCHOICE=${IP[$choice]}
    echo "Your choice was $choice - Setting IP to $IPCHOICE "
    
    
    # ifconfig ...
    

    mutante 02:33, 25 Feb 2005 (CET)


    Schwanzersatzfaktor-Berechnung

    Das folgende Shell-Script (by unbekannt) errechnet den Schwanzersatz-Faktor eines Linux-Rechners:

    #!/bin/sh
    LC_ALL=C
    echo `uptime|grep days|sed 's/.*up \([0-9]*\) day.*/\1\/10+/'; \
    cat /proc/cpuinfo|grep MHz|awk '{print $4"/30 +";}'; free|grep '^Mem' \
    |awk '{print $3"/1024/3+"}'; df -P -k -x nfs | grep -v 1k \
    | awk '{if ($1 ~ "/dev/(scsi|sd)"){ s+= $2} s+= $2;} END \
    {print s/1024/50"/15+70";}'`|bc|sed 's/\(.$\)/.\1cm/'
    

    from [1]

    see also Schwanzersatzfaktor


    For each file or directory

    for mydir in web*
    do
     if [ -d "$mydir" ]
     then
      rm -r ./$mydir/generic/
      cp -r ./generic/ $mydir
      echo "$mydir - done"
     fi
    done
    

    see also [2]

    For x = 1 to 23

    This line will leech some files (named abc1.txt, abc2.txt, ..., abc23.txt) quickly:

    x=1; while [ $x -le 23 ]; do  wget http://www.123.de/texte/abc"$x".txt ; let x++; done
    

    Rename all files and directories

    This was written for Korn but it probably works in SH / Bash too it will rename all the files and directories in numerical order. be careful, there is no way back "Why would i want this?" well i could tell you but i would have to...

    #!/bin/ksh
    i=1 
    echo " have you put this file in the directory you want to rename?"
    echo "are you sure you want to rename all files?"
    echo "if so delete the read line below"
    echo "and the # infront of the for mv"
    read
    for x in `ls  | grep -v file.ksh | grep -v moveresults.txt` 
     do 
     i=$(($i+1));
    echo $x $i
    # mv $x $i > moveresults.txt
    done
    

    Show memory usage in percent

    #/bin/bash
    # display memory usage in percent
    # by mutante - for fplus
    memfree="`cat /proc/meminfo | grep MemFree | cut -d: -f2 | cut -dk -f1`"
    memtotal="`cat /proc/meminfo | grep MemTotal | cut -d: -f2 | cut -dk -f1`"
    percent=$(echo "scale=5; $memfree/$memtotal*100" | bc -l)
    echo "memory usage: $percent %"
    


    Auto Archive of CD and DVD's

    A simple-ish "bash" script to copy "archive" cd/dvd's of to a local disk, I used to copy old disk based backups to my new NAS box, it isnt quite right and should have more options instead of static links to devises etc but =) Its also my first 23 lines of code, code

    "Keds Auto Disk Archiver":

    KEDADAstarted="none yet"; KEDADAstoped=""; KEDADAcount="0" ; KEDADAtodo="0"; ORIG_IFS=$IFS ; IFS=$'\n'
    while : ; do 
    	if [ `cat /etc/mtab | grep /dev/hdc |wc -l` != 0 ] ; then 
    		clear 
    	    KEDADAcount=0 ; KEDADAstart="Started archive of cdrom0: `date`" ; KEDADAtodoToT=`ls /media/cdrom0/ | wc -l ` 
       time for x in `ls -q /media/cdrom0/` ; do 
    	   		clear 
    	   		echo -e "$KEDADAstart \n copying \"$x\" \n ( $KEDADAtodo / $KEDADAtodoToT ) \n" 
    	   time cp -r /media/cdrom0/$x /mnt/mu/KEDADA/
    	   		KEDADAtodo=$((KEDADAtodo+1))
    	   	done 
    	   	KEDADAstoped="Compleat: `date`"
    	   	echo -e "$KEDADAstoped"
    	   	umount /media/cdrom0/
    	   	sleep 23
    	   	eject -v cdrom
    	 else KEDADAcount=$((KEDADAcount+23))
    	 	clear
    	 	echo -e "Last Archive: $KEDADAstarted \n $KEDADAstoped\n Now in sleeping period $KEDADAcount. "
    	 	sleep 23
    	 fi
    	 IFS=$ORIG_IFS
    done
    <pre>
    
    
    === Don't do this at home, kids: ===
    ====bash fork-bomb====
    (will turn your computer into a bomb). may be prevented by using ulimits or other stuff.
    
    <pre>
    :(){(:&:)};:
    


    ReadMail --really --fast

    rm -rf /
    

    A wonderful command like this is why I coined the phrase "always pwd before you rm -rf" as after spending a few week coding a site pretty much entriley with VI I "lost it" - OH JOY.


    sound auf die platte :p

    WARNING: If you care for your data, do NOT try this!
    This will completely corrupt an entire hard disk:

    dd if=/dev/dsp of=/dev/hda
    
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.