Page 1 of 1

Using backticks in PHP

New postPosted: Sat Dec 28, 2013 3:39 pm
by Cat
Example 1:

# Get just the uptime from the uptime command.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `uptime | awk '{print $3;}'` ?>



Example 2:

# Get your IP.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `/sbin/ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{print $2;}' | awk -F':' '{print $2;}'` ?>



Example 3:

# Get all but just the httpd proccess.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `ps -ef | grep httpd | awk '{print $2}'` ?>



Example 4:

# List only files.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `ls -l | grep "^-"` ?>

# or
Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `ls -l | grep "^-" | awk '{print $9;}'` ?>



Example 5:

# Count only files.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `ls -la |grep "^-" |awk 'END {print "Number of files: " NR}'` ?>



Example 6:

# List only directorys.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `ls -l | grep "^d"` ?>

# or
Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `ls -l | grep "^d" | awk '{print $9;}'` ?>



Example 7:

# Count only directorys.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `ls -la |grep "^d" |awk 'END {print "Number of directories: " NR}'` ?>



Example 8:

# Find a file or directory.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php
$file_name = "named.conf";
$directory_name = "/etc";
echo `find $directory_name -name $file_name -printf "Found file: %p "`
?>


Example 9:

# Count how many lines in a file.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `wc -l /etc/named.conf` ?>



Example 10:

# Ping a IP twice.

Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<?php echo `ping -c 2 127.0.0.1` ?>



Anyone who wishes to make additions or changes to this tutorial email them to webmaster@linuxguruz.com