Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Wednesday, April 10, 2019

[Linux Tips] Bash Shell (I)

1.to declare a new variable:
variable_name=initial_value;
2. in linux, before you run you shell-script, you need to change the mod of script file.
chmod 755 script_name
(This will change the mode of file to owner: rwx, group and others: r-x)
maybe ./script_name is more precisely. This command will add the permission of execution to this script file.
3. use a variable:
$variable_name
4. to do something on all certain files under current directory:
for file in ` ls *[^smp].byu `
do
# do something on every file $file
echo $file
done

if ...
do
else
fi # end of if :)

5. substring:
To take part of a string:
string="test"
substring=${string:1:2}
echo subtring
result: es
note:
substring=${string_variable_name:starting_position:length}
The string starting position is zero-based.

6. Searching and Replacing Substrings within Strings:
In this method you can replace one or more instances of a string with another string. Here is the basic syntax:
alpha="This is a test string in which the word \"test\" is replaced."
beta="${alpha/test/replace}"
The string "beta" now contains an edited version of the original string in which the first case of the word "test" has been replaced by "replace". To replace all cases, not just the first, use this syntax:
beta="${alpha//test/replace}"

Note the double "//" symbol.

Here is an example in which we replace one string with another in a multi-line block of text:

list="cricket frog cat dog"
poem="I wanna be a x\n\
A x is what I'd love to be\n\
If I became a x\n\
How happy I would be.\n"
for critter in $list; do
echo -e ${poem//x/$critter}
done
Run this example:
$ ./myscript.sh
result:
I wanna be a cricket
A cricket is what I'd love to be
If I became a cricket
How happy I would be.
I wanna be a frog
A frog is what I'd love to be
If I became a frog
How happy I would be.
I wanna be a cat
A cat is what I'd love to be
If I became a cat
How happy I would be.
I wanna be a dog
A dog is what I'd love to be
If I became a dog
How happy I would be.
Silly example, huh? It should be obvious that this search & replace capability could have many more useful purposes.

7. to run matlab (e.g plot some graph) without start matlab window
matlab -nodesktop -nosplash -r "plot_data;quit;"
#here we have a .m file named "plot_data.m" under current dir

Monday, April 1, 2019

[Linux Tips] Screen, date

[Linux Tips] includes a series of very common tips for development in Linux environment. By default, I am using Ubuntu.

Screen.
This is used to create/maintain a session. This is very useful when you ssh to a remote machine and running some jobs in a interactive mode. Imagine if your network is disconnected, the thread to run your job will be killed. Screen can help you to create a session. You can attach (enter) or detach (quit) from the session.
1. To create a session
screen -S session_name
or simply,
screen

2. To list all sessions available.
screen -ls

3. Attach a session
screen -r session_name (a prefix is enough)

4. Detach a session
ctrl+a+d

5. Delete a session
screen -X -S session_name kill


Date
When you run some bash script. It's common that you want to name your output with current date/time.
In bash shell script you can do it as below.
dt_suffix=`date +'%d%m%H%M'`
Basically, it just assigns the date/time to a variable. And the string comes from date command. The +"%d%m%H%M" is the optional format parameter.


Bash Shell script, some common usages.
For-loop with an array.

declare -a your_array=(
foo
bar
baz
)
array_len=${#your_array[@]}
for ((i = 0; i< ${array_len}; i++));
do
echo "processing" ${your_array[${i}]}
# Do something here.
done

Check whether directory or file already exists.
file_path=PathToYourFile
dir_path=PathToYourDir
if [ ! -f ${file_path} ]; then
echo "File ${file_path} does not exist!"
else
echo "File ${file_path} already exists!"
fi

if [ ! -d ${dir_path} ]; then
echo "Directory ${dir_path} does not exist!"
else
echo "Directory ${dir_path} already exists!"
fi