Posts

Showing posts with the label Arg

checking to see if an arg is a file and printing accordingly

 You can check if a command-line argument is a file in a shell script and print a message accordingly using conditional statements. Here's an example using bash: ```bash #!/bin/bash # Check if an argument was provided if [ $# -eq 0 ]; then     echo "No argument provided. Please specify a file."     exit 1 fi # Check if the argument is a file if [ -f "$1" ]; then     echo "$1 is a file." else     echo "$1 is not a file." fi ``` This script checks if an argument is provided and then uses the `-f` option within square brackets `[ ]` to test if the argument is a file. If it is, it prints a message indicating that it's a file; otherwise, it prints a message indicating that it's not a file. Save this script to a file (e.g., `check_file.sh`), make it executable with `chmod +x check_file.sh`, and then run it with a command-line argument: ```bash ./check_file.sh somefile.txt ``` Replace `somefile.txt` with the argument you want to check. The s