Pages

Showing posts with label bash scripting. Show all posts
Showing posts with label bash scripting. Show all posts

Wednesday, March 28, 2012

Arithmetic equations in bash

Basic equations in bash scripts:

x=15
y=8
sum=$((x+y))

dif=$((x-y))

mod=$((x%y))

prod=$((x*y))

Friday, February 24, 2012

How to remove/replace a given character/word from all the filenames in current directory using sed

#!/bin/bash
extension="txt"
for files in *.$extension; 
do 
    mv $files `echo $files | sed 's/B/A/g'`; 
done;
 
The mv command is used to rename the files.

sed parses the filename and replaces all the occurrences of "B" character with "A".

eg. BAOBAB.txt will be replaced by AAOAAA.txt

The script is case sensitive. Also, be careful with special characters that need to be escaped with "\".
For example if you have a file called ^name.txt and want to replace "^" character from the filename with "A", using the command:

mv $files `echo $files | sed 's/^/A/g'`;  

will lead to the following output:

A^name.txt

You need to escape "^" character:


mv $files `echo $files | sed 's/\^/A/g'`;

In this case the output will be:

Aname.txt

This script also works if you use words instead of characters.

Friday, June 4, 2010

Setup the Proxy Server for apt-get in Linux

        Open a new root terminal and run the next command from $HOME folder:

gedit .bashrc

        This will open the /etc/bash.bashrc file for editing. Add the following lines to the end of this file:
export http_proxy=http://user:password@my.proxy.server:port/
export ftp_proxy=http://user:password@my.proxy.server:port/
        If you have no authentication on the proxy server, include only the server and port:
export http_proxy=http://cache.proxy.server:port/
export ftp_proxy=
http://cache.proxy.server:port/
 
        Finally,  run the source command:

source .bashrc 

        Now you should be able to run apt-get from the command line.

Thursday, June 3, 2010

Comenzi utile Linux

man (command)= info (command)
w = display user list
pwd = print working directory
mkdir = create new folder
ls display files
xrandr = screen resolution
touch = create file
ps = see all processes
echo = display message on screen