Hướng dẫn tạo file binary từ shell script trên linux

Hướng dẫn tạo file binary từ shell script

 

Làm việc với môi trường linux các bạn sẽ thấy có nhiều file binary đã được mã hoá dạng nhị phân lưu trữ ở​​ /bin, /sbin , /usr/bin, /usr/sbin….​​ Mình sẽ hướng dẫn thử tạo 1 file binary như nào.

Step 1 –​​ Yêu cầu

First of all, You need to install required packages for SHC compiler.

For Ubuntu, Debian and LinuxMint

sudo apt-get install libc6-dev​​ 

For CentOS, RHEL & Fedora

sudo yum install glibc-devel

Step 2 – Download and Install SHC

Download the latest source code of SHC compiler from its official webpage or using below commands and extract on your system.

cd /usr/src

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz

sudo tar xzf shc-3.8.9.tgz

Now compile the SHC source code on your system and install it using following command.

cd shc-3.8.9

make

make install

Step 3 – Create Shell Script

Let’s create a shell script as per your requirement or skip this step if you already created. For this article we have created below sample script which add the integer values given on command line parameter and show the sum of them.

vim script.sh

 

#!/bin/bash

 

total=0

 

for i in $@; do

 ​​ ​​​​ if [ ! -z "${i##[0-9]*}" ]; then

echo "Please enter numeric only"

exit 1

 ​​ ​​​​ fi

 ​​ ​​​​ total=$(($total + $i))

done

 

if [ $total -eq 0 ]; then

 ​​ ​​ ​​​​ echo "Plesae execute script like: $0 10 20 30"

 ​​ ​​ ​​​​ exit 0

fi

 

echo $total

Step 4 – Create Binary of Script

At this stage we have installed SHC compiler and have a shell script named script.sh. Use the following command to create binary file of your script.

shc -T -f script.sh

The above command will create two files in current directory. One will be script.sh.x.c which is in C language format of your script. Second one will be script.sh.x which will be in binary format.

Step 5 – Test Binary Script:

If you try to open binary format of script, you will see that it is not in human readable format.

Now move this script under /usr/bin directory to use from anywhere in system. Also remove .sh.x from file name. So it will be available with simple name. Also set the execute permissions to everyone

mv script.sh.x /usr/bin/script

chmod +x /usr/bin/script

Now type command ‘script’ from anywhere in system. You will see the same results as your shell script does.

script 10 20 30​​ 

60

 

Lưu ý:

Trên Ubuntu gõ lệnh

​​ apt-get install shc​​ 

là có thể gõ lệnh shc –T –f file.sh

để tạo file binary thành công.

Khi tạo 1 file binary bắt buộc có lệnh dưới​​ ở trong file (lệnh này không phải là comment!)

#!/bin/bash

 

Lệnh này là chỉ định ra /bin/bash nghĩa là dùng bash để chạy lệnh

Câu lệnh​​ 

shc -T -f script.sh

​​ sẽ tạo ra 1 file tên là script dạng binary

​​ bất kì nơi nào cũng có thể gõ được lệnh script và chạy thực thi câu lệnh trong nội dung của script

 

Link tham khảo

#https://tecadmin.net/create-binary-file-from-shell-script/

chúc các bạn thành công!

BÀI VIẾT CÙNG CHUYÊN MỤC

CẢNH BÁO BẢO MẬT: Lỗ Hổng Nghiêm Trọng CVE-2026-42533 Trên NGINX – Cập Nhật Ngay Để Tránh Rủi Ro Bị Tấn Công
CẢNH BÁO BẢO MẬT: Lỗ Hổng Nghiêm Trọng CVE-2026-42533 ...

CẢNH BÁO BẢO MẬT: Lỗ Hổng Nghiêm Trọng CVE-2026-42533 Trên NGINX – Cập Nhật ...

BACKUP DỮ LIỆU LỚN TỪ LINUX UBUNTU 24.04 LTS LÊN GOOGLE DRIVE AN TOÀN, TỐI ƯU BĂNG THÔNG
BACKUP DỮ LIỆU LỚN TỪ LINUX UBUNTU 24.04 LTS LÊN ...

BACKUP DỮ LIỆU LỚN TỪ LINUX UBUNTU 24.04 LTS LÊN GOOGLE DRIVE AN TOÀN, TỐI ƯU ...

Top 5 hệ thống LMS tốt nhất năm 2026 – Giải pháp quản lý học tập hiện đại cho trường học và doanh nghiệp
Top 5 hệ thống LMS tốt nhất năm 2026 – Giải pháp quản ...

Top 5 hệ thống LMS tốt nhất năm 2026 – Giải pháp quản lý học tập hiện đại cho ...

Hướng dẫn cấu hình SSL Let's Encrypt nhiều Website IIS bằng Win-ACME (Windows Server 2025)
Hướng dẫn cấu hình SSL Let's Encrypt nhiều Website IIS ...

Hướng dẫn cấu hình SSL Let's Encrypt nhiều Website IIS bằng Win-ACME (Windows ...

Hướng Dẫn Sửa Lỗi Không Extend Được Ổ C Trên Windows Server 2025 Do Vướng Phân Vùng Recovery
Hướng Dẫn Sửa Lỗi Không Extend Được Ổ C Trên Windows ...

Hướng Dẫn Sửa Lỗi Không Extend Được Ổ C Trên Windows Server 2025 Do Vướng Phân ...

Cảnh Báo Đỏ: Chiến Dịch FortiBleed Rò Rỉ Hàng Chục Nghìn Thông Tin Quản Trị Tường Lửa Fortinet
Cảnh Báo Đỏ: Chiến Dịch FortiBleed Rò Rỉ Hàng Chục ...

Cảnh Báo Đỏ: Chiến Dịch FortiBleed Rò Rỉ Hàng Chục Nghìn Thông Tin Quản Trị ...