The source code for this blog is available on GitHub.
Note
Top

Bash: An Introduction to the Unix Shell and Command Language

Cover Image for Bash: An Introduction to the Unix Shell and Command Language
Chen Han
Chen Han

Bash

Bash is a Unix shell and command language that is commonly used on Linux and other Unix-like operating systems. It allows you to run commands and scripts, perform operations on files and directories, and manage system tasks.

A Bash script is a text file that contains a sequence of commands that are executed by the Bash interpreter. Bash scripts are typically used to automate tasks or to perform actions that involve multiple commands.

To create a Bash script, you can use a text editor to create a file with a .sh extension and then add your commands to the file. For example, you might create a file called "myscript.sh" that contains the following commands:

#!/bin/bash

echo "Hello, world!"
mkdir new_directory
touch new_file

To make the script executable, you need to give it execute permissions with the chmod command:

chmod +x myscript.sh

Then you can run the script with:

./myscript.sh

This will execute the commands in the script, in order.

Bash scripts can also accept arguments, which are passed to the script when it is called. For example, you might create a script that takes a file name as an argument and performs some operation on the file:

#!/bin/bash

filename=$1

echo "Processing file: $filename"
wc -l "$filename"

You can then run the script and pass in a file name as an argument:

./myscript.sh myfile.txt

This will execute the script and pass the value "myfile.txt" as the first argument to the script. The script can then use the value of the $1 variable to perform an operation on the file.

Bash scripts can also include control structures, such as loops and conditional statements, to perform different actions based on different conditions. For example, you might create a script that loops over a set of files and performs an operation on each file:

#!/bin/bash

for file in *.txt; do
  echo "Processing file: $file"
  wc -l "$file"
done

This script will iterate over all the .txt files in the current directory and execute the wc command on each file.

Bash scripts are a powerful tool for automating tasks and can be used to perform a wide variety of tasks on a Linux or Unix system.

Batch Rename the Image Files

To batch rename the image files in a directory, you can use a script similar to the following

#!/bin/bash

# Set the start number for the new filenames
num=0

# Iterate over all .jpg files in the current directory
for f in *.png; do
  # Increment the number
  num=$((num + 1))

  # Rename the file using the current number
  mv -- "$f" "voyage-$num.png"
done

This script will iterate over all the .jpg files in the current directory and rename them using the pattern "voyage-X.png", where X is the current number. The number is incremented for each file.

batch rename directory and its subdirectories

To batch rename both .jpg and .png files in a directory and its subdirectories, keeping the renamed files inside their respective directories, you can use a script similar to the following:

#!/bin/bash

# Set the start number for the new filenames
num=0

# Iterate over all .jpg and .png files in the current directory and its subdirectories
find . -type f -name '*.jpg' -o -name '*.png' | while read file; do
  # Increment the number
  num=$((num + 1))

  # Get the current directory name
  dir=$(dirname "$file")

  # Get the file extension
  ext=${file##*.}

  # Rename the file using the current directory name, number, and extension
  mv -- "$file" "$dir/$dir-$num.$ext"
done

This script will iterate over all the .jpg and .png files in the current directory and its subdirectories, and rename them using the pattern "dirname/dirname-X.ext", where "dirname" is the name of the current directory, X is the current number, and "ext" is the file extension. The number is incremented for each file.

Kill the process in specific port

It shows an error message indicating that the process is unable to bind to port 3035 because it is already in use. This error occurs when another process is already using that port and the current process is unable to bind to it.

started with pid 38521
 started with pid 38522
 started with pid 38523
 ✖ 「wds」:  Error: listen EADDRINUSE: address already in use 127.0.0.1:3035
     at Server.setupListenHandle [as _listen2] (net.js:1331:16)
     at listenInCluster (net.js:1379:12)
     at GetAddrInfoReqWrap.doListen [as callback] (net.js:1516:7)
     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:73:8) {
   code: 'EADDRINUSE',
   errno: -48,
   syscall: 'listen',
   address: '127.0.0.1',
   port: 3035
 }

To fix this issue, you can use the ps and grep commands to find the process that is using the port and then use the kill command to terminate it. The ps command lists the processes running on the system, and the grep command filters the output to show only the processes that contain the specified string (in this case, node).

ps aux | grep node

ibiza            11387   0.0  0.1  4860556   4780 s000  S+   10:59AM   0:16.34 node /Users/han/liteshop/node_modules/.bin/webpack-dev-server --config /Users/han/liteshop/config/webpack/development.js
ibiza             3720   0.0  0.0  4715000    520   ??  S    10:53AM   0:05.74 /usr/local/bin/node /Users/han/... true true

Once you have identified the process IDs of the processes that are using the port, you can use the kill command to terminate them. The -9 option specifies that the process should be terminated immediately, rather than being sent a signal to terminate gracefully.

kill -9 11387
kill -9 3720

The second block of code shows an error message indicating that port 3000 is already in use. To fix this issue, you can use the lsof command to find the process that is using the port and then use the kill command to terminate it. The lsof command lists the open files on the system, and the -i option filters the output to show only the files that are associated with internet sockets (such as TCP or UDP sockets).

Once you have identified the process IDs of the processes that are using the port, you can use the kill command to terminate them as described above.

error: Port 3000 is already in use

lsof -i :3000
kill 28967 41613

Delete lines containing a specific string [上午7:29] Han Chen

To delete lines containing a specific string, such as [上午7:29] Han Chen, from a file or input text, you can use the sed command in a Unix-like operating system, such as macOS. The sed command is a powerful tool for editing text files from the command line.

Here is an example of how you can use the sed command to delete lines containing the string [上午7:29] Han Chen from a file called input.txt:

Copy code
sed '/\[上午7:29] Han Chen/d' input.txt > output.txt

This command will read the input.txt file, delete all lines containing the specified string, and write the result to a new file called output.txt. The /d option specifies that the lines should be deleted. The / characters are used to enclose the search pattern, which in this case is the string [上午7:29] Han Chen.

If you want to delete the lines in place, without creating a new output file, you can use the -i option to edit the file in place:

Copy code
sed -i '/\[上午7:29] Han Chen/d' input.txt

This command will delete the lines containing the specified string from the input.txt file directly.

© 2024 WOOTHINK. All Rights Reserved.
Site MapTerms and ConditionsPrivacy PolicyCookie Policy