while loop in csh

This made my day, while loop in one line -one liner- under csh

printf “while ( 1 ) \n df -m \n echo ‘remaining space’ \n  sleep 60 \n end” | csh -f

 Of course, you could install bash but that can make our system vulnerable and also we’d loose the fun of using csh & tcsh

compare “n” number of variables

An easy way to compare “n” number of variables in bash, precisely to find if all of them are equal or not, can be easily done in the following way:

while [ $i -lt 10 ]; do
Sequence[$i]=whatever sequence you want
let i=$i+1
done

for i in ${Sequence[*]}; do
[ "${Sequence[0]}" = "$i" ]
j=$(( $? + j ))
done

if [ $j -eq 0 ];
echo="they are all the same"
fi

Interest Calculator

Recently I found myself calculating interest and interest duration, I heaven’t found any decent tool for this so I did one by myself in C++. It does nothing else but prints the numbers you were looking for right in your terminal window.

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

class Interest_C
{
private:

float round (float value)
{
return ((int)(value * 100 + .5) / 100.0);
}

public:

float FinalSum(float isum, float percent, float deposit, int period)
{
float SUM = isum;
int i = 0;

do {
SUM = SUM + deposit + ((SUM * percent / 100))/12; because we the interest is for a whole year
cout << i+1 << “. month” << endl;
cout.setf(ios::fixed);
cout << setprecision(0) << round(SUM) << endl;
cout << “Interest = ” << round(((SUM * percent / 100))/12) << endl;
cout << endl;
i++;
}
while (i != period);

return SUM;
}

int FinalPeriod(float isum, float percent, float deposit, float fsum)
{
float SUM = isum;
int i = 0;
do {
SUM = SUM + deposit + ((SUM * percent / 100))/12; //we have to divide by 12 because we the interest is for a whole year
cout << i+1 << “. month” << endl;
cout.setf(ios::fixed);
cout << setprecision(0) << round(SUM) << endl;
cout << “Interest = ” << round(((SUM * percent / 100))/12) << endl;
cout << endl;
i++;
}
while (SUM <= fsum);
return SUM;
}

};

struct myseps : numpunct<char> {
char do_thousands_sep() const { return ‘,’; }
string do_grouping() const { return “\3”; }
};

int main()

{
float initial, p, dep, sum;
int period;
Interest_C *Interest = new Interest_C();
cout.imbue(locale(locale(), new myseps));

char menu = 0;

while (menu != 1)
{
cout << “1 – Find out the sum” <<endl;
cout << “2 – Find out the period” << endl;
cout << “0 – exit” << endl;
char menu;
cout << “1 or 2? “;
cin >> menu;
getchar();

switch (menu)
{
case ‘1’:
cout <<“Initial amount: ” ; cin >> initial;
cout <<“Interest (%): “; cin >> p;
cout <<“Monthly deposit: “; cin >> dep;
cout <<“Period (months): “; cin >> period;
if (period <= 0) period = 1;
Interest->FinalSum(initial, p, dep, period);
break;

case ‘2’:
cout <<“Initial amount: ” ; cin >> initial;
cout <<“Interest (%): “; cin >> p;
cout <<“Monthly deposit: “; cin >> dep;
cout <<“Final sum: “; cin >> sum;
Interest->FinalPeriod(initial, p, dep, sum);
break;

case ‘0’:
return 0;
break;
}
}

delete Interest;
return 0;
}

Multiple rename in bash

In case you are wondering how to rename multiple files in bash here is an idea:

for file in ?????*; do mv $file `echo $file | cut -c4-`; done

Whit this you can cut 4 characters from the beginning of the file names which are at least six characters long.

Btw, this will NOT work if you have spaces in your filenames, but to solve this you can issue the command below replacing spaces with underscores:

for file in *\ *; do mv $file ${file// /_}; done

Enjoy 😉