Here are the steps to create and execute the zsh script
Step 1: Open a terminal window.
Step 2: Use the nano or vi commands to open a new blank file in a text editor. For example: nano myscript.zsh
Step 3: Type the zsh commands that you want to include in the script.
#!/bin/zsh # This line sets the variable 'filename' to the first argument passed to the script filename=$1 # This line prints the hostname of the current system echo "Hostname: $(hostname)" # This line creates a subshell and runs the 'ls' command inside it. # The output of the 'ls' command is then stored in the 'ls_output' variable ls_output=$(ls) echo "Output of 'ls' command: $ls_output" # This line checks if the file specified by the 'filename' variable exists # and is a regular file (not a directory). If the file exists, it prints the contents # of the file using the 'cat' command. If the file does not exist, it prints an error message if [[ -f "$filename" ]]; then cat "$filename" else echo "Error: File not found" fi # This line prints the current working directory echo "Current working directory: $(pwd)" # This line prints the current user's home directory echo "Home directory: $HOME" # This line reads input from the user and stores it in the 'input' variable echo -n "Enter your name: " read input # This line prints the value of the 'input' variable echo "Your name is: $input"
Step 4: Save the file by pressing CTRL + X, then Y, and then Enter.
Step 5: Make the script executable by running the chmod command: chmod +x myscript.zsh
$ chmod +x myscript.zsh
Step 6: Run the script by typing its name at the command prompt, followed by any arguments that the script expects: ./myscript.zsh argument1 argument2
./myscript.zsh argument1 argument2
Here is the output of the above zsh script after executing it:
Hostname: myhostname Output of 'ls' command: test.txt Hello, World! Current working directory: /home/user/documents Home directory: /home/user Enter your name: John Your name is: John
Creating and Running bash and zsh Scripts
Creating and running scripts can be a powerful tool for automating tasks and streamlining workflow. Bash and Zsh are two popular shells that can be used to create and run scripts on Linux and macOS systems. Bash, which stands for Bourne-Again Shell, is the default shell on most Linux distributions and macOS. Zsh, or Z shell, is a more feature-rich alternative to Bash that offers improved tab completion and command history among other features. This guide will discuss how to create and run scripts using both Bash and Zsh, including basic syntax and examples of common tasks that can be automated with scripts.
To create a script, you will first need to open a text editor and type in the commands you want the script to execute. It is important to make sure that the script is saved with the .sh extension for Bash or .zsh extension for Zsh. Once your script is saved, you will need to make it executable by running the command “chmod +x [scriptname]” in the terminal.
To run the script, you can use the terminal command “./[scriptname]”. If your script is located in a directory that is not in your system’s PATH, you will need to specify the full path to the script when running it. For example, if your script is located in the Documents folder, you would run “./Documents/[scriptname]”.
Contact Us