Pages

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

Sunday, October 18, 2020

Fedora 32 : About positive and negative lookahead with Bash commands.

Today I will talk about something more complex in Linux commands called: positive and negative lookahead.
This solution can be found in several programming languages including Bash
The lookahead process is part of regular expressions.
The lookahead process looks ahead in the string and sees if it matches the given pattern, but then disregard it and move on.
It is very useful when we want to go through the strings.
The lookahead process can be both positive and negative depending on the purpose.
Negative lookahead is indispensable if you want to match something not followed by something else and looks like this:
q(?!s).
The string is the question q is analyzed and if it does not match and is not followed by s returns the result.
The positive lookahead it works the same way only now it is parsed if it corresponds to s.
The positive lookahead looks like this:
q(?=s)
Let's look at a simple example of detecting the PAE option for the processor.
We can use this command but we will find a lot of information ...
[root@desk mythcat]# cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 58
model name	: Intel(R) Celeron(R) CPU G1620 @ 2.70GHz
stepping	: 9
...
In some cases, the resulting information can be taken using pipe and grep but they will be increasingly fragmented.
I will use the same command cpuinfo and we will look for the pae information in the flags.
All CPU flags can be found here.
Let's prove with internal lookahead to find the pae flag.
[root@desk mythcat]# cat /proc/cpuinfo | grep -oP '(?='pae')...' 
pae
pae
This result gives me additional information, namely that there are two cores.
Do you have a question?

Sunday, November 10, 2019

Fedora 31 : Use bash script to see all the permissions.

This tutorial will show you how to generate all the permissions using one file named file_test.
[mythcat@desk ~]$ mkdir my_bash_scripts
[mythcat@desk ~]$ cd my_bash_scripts/
[mythcat@desk my_bash_scripts]$ vim all_permissions.sh
[mythcat@desk my_bash_scripts]$ vim all_permissions.sh
[mythcat@desk my_bash_scripts]$ chmod +x all_permissions.sh
[mythcat@desk my_bash_scripts]$ ls
all_permissions.sh
Let's see the bash script file named all_permissions.sh:
#!/bin/sh
#loops through a chmod sequence

count_perm(){
        foo=1
        touch file_test
        while [ "$foo" -ne 7778 ];do
                echo $foo >> out
                chmod $foo file_test
                ls -gG >> out
                foo=$(($foo+1))
        done
}
count_perm
egrep -v '(all_permissions.sh)|(total)|(out)' out > results
echo "use command cat results to see permissions for file_test."

exit 0
You can run the script:
[mythcat@desk my_bash_scripts]$ ./all_permissions.sh 
...
Try 'chmod --help' for more information.
chmod: invalid mode: ‘7768’
Try 'chmod --help' for more information.
chmod: invalid mode: ‘7769’
Try 'chmod --help' for more information.
use command cat results to see permissions for file_test.
[mythcat@desk my_bash_scripts]$ ls
all_permissions.sh  file_test  out  results
The out file show all permissions for files:
[mythcat@desk my_bash_scripts]$ cat out
...
7774
total 2052
-rwxrwxr-x. 1     331 Nov 10 18:22 all_permissions.sh
-rwsrwsr-T. 1       0 Nov 10 18:23 file_test
-rw-rw-r--. 1 1153701 Nov 10 18:23 r
7775
total 2052
-rwxrwxr-x. 1     331 Nov 10 18:22 all_permissions.sh
-rwsrwsr-t. 1       0 Nov 10 18:23 file_test
-rw-rw-r--. 1 1153853 Nov 10 18:23 r
7776
total 2052
-rwxrwxr-x. 1     331 Nov 10 18:22 all_permissions.sh
-rwsrwsrwT. 1       0 Nov 10 18:23 file_test
-rw-rw-r--. 1 1154005 Nov 10 18:23 r
7777
total 2052
-rwxrwxr-x. 1     331 Nov 10 18:22 all_permissions.sh
-rwsrwsrwt. 1       0 Nov 10 18:23 file_test
-rw-rw-r--. 1 1154157 Nov 10 18:23 r
The results file show the permissions:
[mythcat@desk my_bash_scripts]$ cat results 
...
7766
-rwsrwSrwT. 1       0 Nov 10 18:30 file_test
7767
-rwsrwSrwt. 1       0 Nov 10 18:30 file_test
7768
-rwsrwSrwt. 1       0 Nov 10 18:30 file_test
7769
-rwsrwSrwt. 1       0 Nov 10 18:30 file_test
7770
-rwsrws--T. 1       0 Nov 10 18:30 file_test
7771
-rwsrws--t. 1       0 Nov 10 18:30 file_test
7772
-rwsrws-wT. 1       0 Nov 10 18:30 file_test
7773
-rwsrws-wt. 1       0 Nov 10 18:30 file_test
7774
-rwsrwsr-T. 1       0 Nov 10 18:30 file_test
7775
-rwsrwsr-t. 1       0 Nov 10 18:30 file_test
7776
-rwsrwsrwT. 1       0 Nov 10 18:30 file_test
7777
-rwsrwsrwt. 1       0 Nov 10 18:30 file_test

Monday, January 16, 2017

Windows Subsystem for Linux (beta)

This is the first release of Bash on Windows and it is branded "beta" deliberately - it's not yet complete! You should expect many things to work and for some things to fail! We greatly appreciate you using Bash on Windows and helping us identify the issues we need to fix in order to deliver a great experience.
Try this tutorial.
The Fedora don't have a docker userspace, but Suse come with this feature.

Thursday, June 11, 2015

Fix Bash shell vulnerability under Linux OS .

If some Bash code outside of the curly brace then it will then be executed by the linux system.
env x='() { :;}; echo vulnerable' bash -c " bash test "
If your system is vulnerable, it will then output:
vulnerable
bash test 
How Do You Fix It ? Just open up a terminal, and type:
sudo apt-get update
sudo apt-get upgrade
or under Fedora with :
sudo yum update
Also you can see more about linux and linux tutorials here.

Thursday, July 25, 2013

Using cat command to see all source code from one folder.

You can see all content of your files using cat command.

I created one function named catt in my .bashrc file.

This is the function you can change to show any type of files not just javascript.

That allow you to working in same folder with many files and see the changes.

If you have another idea about how to see changes in all source of code then just send me one mail or comment.

function catt(){
    for i in *.js;
        do echo "__""$i""__";
        echo "--------------------------";
        cat "$i" -n -b;
        echo "--------------------------";
        done ;
}

And this is result output for all javascript from my node.js folder.

--------------------------
__serv.js__
--------------------------
     1 var sys = require("sys"),
     2 my_http = require("http");
     3 my_http.createServer(function(request,response){
     4  sys.puts("I got kicked");
     5  response.writeHeader(200, {"Content-Type": "text/plain"});
     6  response.write("Hello World");
     7  response.end();
     8 }).listen(8080);
     9 sys.puts("Server Running on 8080");
--------------------------
__test.js__
--------------------------
     1 var sys = require("sys");
     2 sys.puts("Hello World");
--------------------------

One problem can be the number of files parse by this function.

In this case you will not able to see all content of files. Will be to much for you.

But for settings files will work great.

Saturday, November 26, 2011

Protect your root account.

I will present a solution to protect the root account, quite funny but still elegant.
You can read about the command trap using: man trap.
What is this command?
when the specified event will occur will execute the command specified.
In this case, it will receive signal respectively will execute one command - exit
The source code.
 protect()
 {
 echo "What is the secret ?"
 trap protect 2 20
 read -s resp
 if [ "$resp" != "asd" ]; then
 echo "Error!"
exit
fi
 }
 protect
The result will be leaving the root account if you do not answer the question correctly.
Now protect the file against unauthorized changes:
# chmod 700 /root/.bashrc
You can create using the example above, different ways to execute various commands.

Sunday, August 22, 2010

Create html file with bash script using awk

We will present a more special method of using the Linux command line.
This method comes in helping those who need to text or HTML content.
In fact, a combination of Linux commands to get an output.
Suppose we have a list of jpeg files in folder image.
They should exist into HTML to be displayed.
As we know an image has the form:
<img src="image/name_of_jpeg.jpg" alt="" /> 
The list of file is :
11.jpg,12.jpg,13.jpg,14.jpg,15.jpg
My method uses two commands: ll and awk.
First, I will display files:
$ ll | awk '/jpg/ {print $9}'
11.jpg
12.jpg
13.jpg
14.jpg
15.jpg 
I created one variable f1.
In these variables put the beginning of each line:
f1=‘<img src="image‘; 
We add two sed commands.
First, remove the space before the file name.
The second will add the rest to complete the link.
The xargs command is used with echo and print the first part of the link.
Finally we use <<< aaaa.html to output the HTML file.
The full script is here .