Blog links

Showing posts with label commands. Show all posts
Showing posts with label commands. Show all posts

Linux Hack, Learn Interesting commands and Play with Linux


 

Trick 1: Perform mkdir and cd using a single command

Sometimes when you create a new directory, you may cd to the new directory immediately to perform some work as shown below.


# mkdir -p /tmp/subdir1/subdir2/subdir3

# cd /tmp/subdir1/subdir2/subdir3

# pwd
/tmp/subdir1/subdir2/subdir3
Wouldn’t it be nice to combine both mkdir and cd in a single command? Add the following to the .bash_profile and re-login.

$ vi .bash_profile

function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }

Now, perform both mkdir and cd at the same time using a single command as shown below:

# mkdircd /tmp/subdir1/subdir2/subdir3

[Note: This creates the directory and cd to it automatically]

# pwd
/tmp/subdir1/subdir2/subdir3
 
Trick 2: Prompt Command
Bash shell executes the content of the PROMPT_COMMAND just before displaying the PS1 variable.

ramesh@dev-db ~> export PROMPT_COMMAND="date +%H:%M:%S"

22:08:42
ramesh@dev-db ~>

[Note: This displays the PROMPT_COMMAND and PS1 output on different lines]
If you want to display the value of PROMPT_COMMAND in the same line as the PS1, use the echo -n as shown below.
ramesh@dev-db ~> export PROMPT_COMMAND="echo -n [$(date +%H:%M:%S)]"

[22:08:51]ramesh@dev-db ~>

[Note: This displays the PROMPT_COMMAND and PS1 output on the same line]
 

Trick 3: Change color of prompt using tput command

You can also change color of the PS1 prompt using tput as shown below:


$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]“

tput Color Capabilities:

  • tput setab [1-7] – Set a background color using ANSI escape
  • tput setb [1-7] – Set a background color
  • tput setaf [1-7] – Set a foreground color using ANSI escape
  • tput setf [1-7] – Set a foreground color

tput Text Mode Capabilities:

  • tput bold – Set bold mode
  • tput dim – turn on half-bright mode
  • tput smul – begin underline mode
  • tput rmul – exit underline mode
  • tput rev – Turn on reverse mode
  • tput smso – Enter standout mode (bold on rxvt)
  • tput rmso – Exit standout mode
  • tput sgr0 – Turn off all attributes

Color Code for tput:

  • 0 – Black
  • 1 – Red
  • 2 – Green
  • 3 – Yellow
  • 4 – Blue
  • 5 – Magenta
  • 6 – Cyan
  • 7 – White
Trick 4: Change the history file name using HISTFILE


By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file. I’m yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.


# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior

Read more

Write VB Windows Scripts Easier & Faster With VbsEdit


write windows scriptWriting a script to automate various tasks on your system that you would normally have to do manually is something that I’ve become infatuated with recently. Maybe it’s the automation engineer in me, but the idea of having a computer intelligently do what I don’t have time to do is very appealing. That is why I’ve recently started offering some tips on how you can automate your own computer with windows host scripts, typically using Visual Basic script to do things like automate telnet commands or even sending emails.
The reason I like VB script so much is because it’s a scaled down version of Visual Basic itself, which in my opinion is probably one of the easiest programming languages to learn if you’re just getting started as a programmer. That isn’t because the language is limited in features or scaled down in functionality, it’s because using the language is intuitive, and full of objects and components that you can pull in to perform some pretty amazing tasks with your computer.

A VB Scripting Environment

If you want to become a power Windows scripting expert, then you really need an environment that helps you remember the syntax of the language, and provides you with a quick library of functions and objects that are available to you.

VbsEdit Helps You Write Powerful Windows Scripts

If you are writing in VB6 or .Net, then you need to use the Microsoft Studio package that includes the programming environment you need. However, if you’re just a system administrator or a network IT guy, you really don’t want to spend money on an expensive tool just for writing Windows scripts. That’s where VbsEdit comes in handy.
write windows script
Last week I wrote about the BlueFish editor for web apps. Well, VbsEdit is sort of the all-star tool that is very well-known within the IT community for power-scripting highly functional VB based Windows Scripts. As you can see in the screenshot above, using an editor built specifically for VB scripting gives you the power of color-coded, intelligent sensing of declarations, variables, functions and objects. It gives you pop-up lists for suggested methods, properties and events for different objects, and a whole lot more.
If you don’t know what the heck I’m talking about here, don’t worry. After using VbsEdit for a while, you’ll quickly learn how the language works and how much functionality you can add to your Windows Scripts if you write them in this language and use VbsEdit.
The one drawback of the free version of VbsEdit is that it does have a nag-screen when you first launch the application.
writing windows scripts
It is a functional, free version of the software, but you have to click “Evaluate” every time you start. If you really like it, I would suggest buying a copy, as it is really inexpensive. Otherwise, just click Evaluate and the software will launch.

VbsEdit Features

There’s a lot available when you use this tool to write Windows Script, but the first thing that I recommend you do when you first launch it is to click on the “WScript” button at the top of the main window. This enables all of the VBS features that I’m going to describe here.
writing windows scripts
The first thing that I want to point out is that when you start defining available objects, like the windows scripting object or IHost for networking, those objects appear in the Object Browser on the right side of the window.
When you drop down the Object Browser, you can see all of the elements that are available to you, and you can drill down within those to see what properties or methods are available. If you’re just learning VB scripting, this is an invaluable resource.
Another nice feature is the drop-down lookup, just like you might be accustomed to if you’ve written programs in MS Visual Studio.
writing windows scripts
Other really nice features of this tool include adding or removing breakpoints. You can do this by clicking to the left of the line where you want to pause the program, or click on the break point icon in the toolbar.
vb windows scripting
Another nice feature that makes this software just as good as MS Visual Studio is the commenting feature. You can comment entire blocks of text simply by highlighting all of it and clicking on the “comment” icon.
vb windows scripting
There is also a really nice debugging feature that most programming editors lack. When you click the run button, the program will stop at your breakpoints and you can step through your program by clicking on the various step icons.
vb windows scripting
At the bottom of the window, you can view active variables and their values while your script is running. When you’re debugging an especially complex windows script, this feature is worth its weight in gold.
The only second drawback to the free version is another nuisance screen that counts down in seconds any time you want to run your script from inside the editor.
Other really nice features of this VB Script editor include (from left to right) the ability to convert your script into an executable, a button to add your newly written code into your own personal “code library”, a useful search button, and finally a series of bookmarking buttons that let you more easily organize and navigate your script.
write windows script
For the most part, VbsEdit is a much better improvement over using just a text editor for writing Windows Scripts. With VbsEdit, you can quickly see what you want to do, and how to do it. You can test your scripts and get them into production much faster than otherwise possible.

Read more

Record the session of commands in a Linux


 How to record the session of commands in a LinuxYou may use lots and lots of commands in LInux. The command line in very powerful. They say “You can do almost anything from the command line in Linux!”, and I agree. Yes, you can do each and everything in Linux using the commands very quickly. If you are unfamiliar with the Linux commands check out our various beginner tutorials in Linux. Let’s focus on this one. We will record all the command we will type in the terminal and save it in a text file.

How to start the recording session?

First of all,  we will start the session that will record or store every commands we will type on the Terminal. Go to terminal and type:
script recordfile.txt
After you hit enter the script has been started.
recordingsession How to record the session of commands in a Linux
Here, recordfile.txt is the file name where our sessions is going to be stored. You can type the filename whatever you want.
The recording has begun. Whatever you type, it is stored in recordfile.txt.

How to stop the session?

To stop the session or the script hit Ctrl+D in your keyboard.
Now, you can see you textfile and find out the commands that has been recorded.

To view the text file:

cat recordfile.txt
How to append the session in the same file?
You have stopped the session, now what if you want to continue the session in that same text file. It’s easy go to terminal and type
script -a recordfile.txt
To stop and view the file, follow the same step.

Read more
2leep.com