Sometimes we have to administrate this operating systems such as FreeBSD / AIX / HP UX or even Mac OS server where by default due to historical reasons or for security bash shell is not avialable. That's not a common scenario but it happens so if as sysadmin we need to create for loops on ksh it is useful to know how to do that, as for loop cycles are one of the most important command line tools the sysadmin swiss army knife kind of.
So how to create a for loop (cycle) in ksh (Korn Shell)?
The most basic example for a KSH loop shell is below:
#!/bin/ksh for i in 1 2 3 4 5 do echo "Welcome $i times" done
Add the content to any file lets say ksh_loop.ksh then make it executable as you do in bash shells
$ chmod +x ksh_loop.ksh $ ksh ksh_loop.ksh
The overall syntax of the for loop ksh command is as follows:
for {Variable} in {lists} do echo ${Variable} done
Hence to list lets say 20 iterations in a loop in ksh you can use something like:
#!/bin/ksh for i in {1..20} do echo "Just a simple echo Command $i times"; # add whatever system commands you like here done
Example for some useful example with KSH loop is to list a directory content so you can execute whatever command you need on each of the files or directories inside
#!/bin/ksh for f in $(ls /tmp/*) do print "Iterating whatever command you like on /tmp dir : $f" done
Other useful for loop iteration would be to print a file content line by line just like it is done in bash shell, you can do that with a small loop like belows:
#!/bin/ksh for iteration_variable in $(cat file_with-your-loved-content-to-iterate.txt) do print "Current iteration like is : $iteration_variable" done
If you're already used too using grep -v "sometring" filename to print everything from a file without the certain grepped string output and you want to do the same to delete lines based on strings without having to output the grepped string to a file and then overwritting the original file:
A much better way to delete an whole line containing a string match from a file is to use sed sed should be the tool of choice especially if you're scripting because sed is especially made for such batch edittings.
Here is how to do delete an entire line based on a given string:
sed –in-place '/some string to search and delete/d' myfilename
It might be a good idea to also create backups just to make sure something doesn't get deleted incidently to do use:
sed –in-place=.bak '/some string to search and delete/d' myfilename
I've been working for a customer that has about 450 hundred servers running all kind of different Web Applications / Mail services / FTP / Web Statistics etc.. I have a single user / password access to all of the servers (using LDAP authentication) – just like many of my colleagues and by default most of servers shell set is Korn Shell (/bin/ksh) is automatically run on ssh login time ( as it is set as the default shell ). I have nothing personally against Korn Shell but as mainly script in Bash Shell last 10+ years and I'm used to Bash's terminal behavior so much, each time I login to the servers I had to run manually run :
/bin/bash
This becomes really annoying, because I have to login to so many servers daily and do various stuff, so I got pissed off at the end and I wanted to change default shell set (only for my current) user to BASH, since there are other users that work on servers and its not a good idea to change globally servers global shell to bash, because such action could hurt some poor colleague love for KSH 🙂
A note to make here is the servers are primary running SuSE SLES / CentOS and Debian and RHEL Linux, but this bash one liner would be working on any other Linux release which has a bash >=2.0, I've tested it exactly on bash 3.2.25.
while read line; do ssh my-user@$line 'echo "export SHELL=/bin/bash ; exec /bin/bash" >> /home/my-user/.profile' < /dev/null; done < all_servers_list.txt
Since SSH client doesn't have a non-interactive way to pass on password from command line (for security reasons), to make above bash one liner loop to deploy bash as a default shell (mass) on all servers I have to paste (my Sotred in Memory Copied password) the SSH remote login server password 450 times each time I',m prompted by SSH to input password manually (this takes about 20) minutes but it is a worthy time, since from then on /bin/bash runs automatically on server login time. One important note to make is the </dev/nullhere is necessery because otherwise ssh is having troubles with reading the host in the loop. If you try running above cmd without </dev/null you, the loop is about to end after first login attempt. Changing default shell to multiple servers is not done only to servers with default ksh, but for any servers with default csh shell or any other obscure shell to bash (if bash is installed) is achieved by above command.
As you can guess above while bash loop, can easily be modified to run any other command to a bunch of hundred or thousand servers of your choise, the only inconvenience you will have is you have to paste the password for each server part of loop to which ssh is creating connection. This example works on Linux but should work fine on any other platform that have /bin/bash (or /usr/local/bin/bash) installed if on FreeBSD / NetBSD, it should work on HP-UX and SunOS / Solaris hosts which have bash installed too.
Once you've launched the loop and added /bin/bash to run on login to check on next login where you're really running bash shell there is the:
echo $SHELL /bin/bash
If you want keep / grep output of shell variable $SHELL:
MYSHELL=`ps -hp $$|awk '{echo $5}'`
Anyhow on some Linux hosts this variable might show bash and still you might be running old shell ksh / csh, the better way to check your SHELL is with ps -p $$ command (nowdays not known by many admins)
ps -p $$ PID TTY TIME CMD 866 pts/0 00:00:00 bash
Now I can happily use bash on all the servers every time I login. Cheers and Thanks God ! 🙂
If you're a Linux administrator you're probably already quite used to watch command which allows to execute a program periodically, showing output fullscreen. Watch is very useful to run a specific command every XXX seconds, and see the results constantly updated. watch is very useful to keep an eye on growing files, i.e.: lets say keep an eye on SQL dump:
watch "ls -al some-dump-file.sql"
or keep an eye on how a directory keeps growing in real time
watch -n 5 "du -hsc /tmp"
Above command would tell watch to refresh du -hsc /tmp on a 5 seconds interval.
So a logical question pops up "Is there a command line equivalent to Linux's watch?"In Windows there is no native command equivalent of Linux watch but there is one liner bat (Batch) script to equivalent to emulate Linux watch in Windows. The Watch like script in Windows OS looks like so:
@ECHO OFF
:loop
tasklist timeout /t 2
goto loop
Use notepad and paste above batch script to any file and save it as whateverfile.bat, running it will make all processes to get listed occuring every 2 seconds (/t 2 – is an argumeent telling the loop to expire on every 2 seconds).
Modify the script to monitor whatever Windows command you like 🙂 Enjoy
The arcitle, points at few ways to DUMP the HTTP headers obtained from user browser. As I'm not proficient with Ruby, Java and AOL Server what catched my attention is a tiny php for loop, which loops through all the HTTP_* browser set variables and prints them out. Here is the PHP script code:
The script is pretty easy to use, just place it in a directory on a WebServer capable of executing php and save it under a name like: show_HTTP_headers.php
If you don't want to bother copy pasting above code, you can also download the dump_HTTP_headers.php script here , rename the dump_HTTP_headers.php.txt to dump_HTTP_headers.php and you're ready to go.
Follow to the respective url to exec the script. I've installed the script on my webserver, so if you are curious of the output the script will be returning check your own browser HTTP set values by clicking here. PHP will produce output like the one in the screenshot you see below, the shot is taken from my Opera browser:
Another sample of the text output the script produce whilst invoked in my Epiphany GNOME browser is:
You see the script returns, plenty of useful information for debugging purposes: HTTP_HOST – Virtual Host Webserver name HTTP_USER_AGENT – The browser exact type useragent returnedHTTP_ACCEPT – the type of MIME applications accepted by the WebServerHTTP_ACCEPT_LANGUAGE – The language types the browser has support for HTTP_ACCEPT_ENCODING – This PHP variable is usually set to gzip or deflate by the browser if the browser has support for webserver returned content gzipping. If HTTP_ACCEPT_ENCODING is there, then this means remote webserver is configured to return its HTML and static files in gzipped form. HTTP_COOKIE – Information about browser cookies, this info can be used for XSS attacks etc. 🙂 HTTP_COOKIE also contains the referrar which in the above case is: __utmz=238407297.1332444980.1586.413.utmcsr=www.pc-freak.net|utmccn=(referral) The Cookie information HTTP var also contains information of the exact link referrar: |utmcmd=referral|utmcct=/blog/
For the sake of comparison show_HTTP_headers.php script output from elinks text browser is like so:
* HTTP_HOST = www.pc-freak.net
* HTTP_USER_AGENT = Links (2.3pre1; Linux 2.6.32-5-amd64 x86_64; 143x42)
* HTTP_ACCEPT = */*
* HTTP_ACCEPT_ENCODING = gzip,deflate * HTTP_ACCEPT_CHARSET = us-ascii, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-10, ISO-8859-13, ISO-8859-14, ISO-8859-15, ISO-8859-16, windows-1250, windows-1251, windows-1252, windows-1256,
windows-1257, cp437, cp737, cp850, cp852, cp866, x-cp866-u, x-mac, x-mac-ce, x-kam-cs, koi8-r, koi8-u, koi8-ru, TCVN-5712, VISCII,utf-8 * HTTP_ACCEPT_LANGUAGE = en,*;q=0.1
* HTTP_CONNECTION = keep-alive
One good reason, why it is good to give this script a run is cause it can help you reveal problems with HTTP headers impoperly set cookies, language encoding problems, security holes etc. Also the script is a good example, for starters in learning PHP programming.
Have you ever been in need to execute some commands scheduled via a crontab, every let’s say 5 seconds?, naturally this is not possible with crontab, however adding a small shell script to loop and execute a command or commands every 5 seconds and setting it up to execute once in a minute through crontab makes this possible. Here is an example shell script that does execute commands every 5 seconds:
#!/bin/bash command1_to_exec='/bin/ls';
command2_to_exec='/bin/pwd';
for i in $(echo 1 2 3 4 5 6 7 8 9 10 11); do
sleep 5;
$command1_to_exec; $command2_to_exec;
done
This script will issue a sleep every 5 seconds and execute the two commands defined as $command1_to_exec and $command2_to_exec
The script can easily be modified to execute on any seconds interval delay, the record to put on cron to use with this script should look something like:
Where of course /path/to/exec_every_5_secs_cmds.sh needs to be modified to a proper script name and path location.
Another way to do the on a number of seconds program / command schedule without using cron at all is setting up an endless loop to run/refresh via /etc/inittab with a number of predefined commands inside. An example endless loop script to run via inittab would look something like:
while [ 1 ]; do
/bin/ls
sleep 5;
done
To run the above sample never ending script using inittab, one needs to add to the end of inittab, some line like:
mine:234:respawn:/path/to/script_name.sh
A quick way to add the line from consone would be with echo:
Then to load up the newly added inittab line, inittab needs to be reloaded with cmd:
# init q
I've also red, some other methods suggested to run programs on a periodic seconds basis using just cron, what I found in stackoverflow.com's as a thread proposed as a solution is:
Here are two really nice videos produced by Daniela Popova.
One is called Black and White Story and the other one is with the funny name To Download an Apple
The videos was produced for her Graduation assignment in NATFA (National Academy of Theater and Film arts). Even better the Black and White Story Video has been selected for the Festival of the Orthodox Christian Cinema in Moscow. The movie was selected by a the jury on the festival (a professor) who realized there is a deeper spiritual meaning behind the Black and White Story
The Black & White Story Video has also an outstanding bulgarian national folklore music combined with some modern day music, just check it out and enjoy.
The second movie To Download an Apple is a humorous one and I believe presents the sometimes stupid and serious efforts we do to follow fake imaginative goals.
As Daniela is a Christian the movies contain also a hidden Christian messages 😉 I greet her for the great work! Considering the uniqueness of the videos it’s obvious Daniela is really talented! I’m looking forward to see some more from her works.