11-04-2025, 09:18 AM
How to run the script:
bash
كود:
./your_script.sh /path/to/input_file.txt[b]Inside [/b]
كود:
your_script.sh[b]:[/b]
You would use the variable
كود:
$1 to refer to the file path.
bash
كود:
#!/bin/bash
INPUT_FILE="$1"
# Check if file was provided and exists
if [ -z "$INPUT_FILE" ]; then
echo "Usage: $0 <input_file>"
exit 1
fi
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: File not found at $INPUT_FILE"
exit 1
fi
# Example: Read the file line by line
while IFS= read -r line; do
echo "Next line: $line"
done < "$INPUT_FILE"Redirecting the file content to standard input
The script reads directly from its standard input (stdin), which the shell connects to the file's content using the
كود:
< operator.
[b]How to run the script:[/b]
bash
كود:
./your_script.sh < /path/to/input_file.txt[b]Inside [/b]
كود:
your_script.sh[b]:[/b]
The script uses commands that read from stdin, such as
كود:
read,
كود:
cat,
كود:
grep,
كود:
tr, etc.
bash
كود:
#!/bin/bash
# Example: Read from standard input line by line
while IFS= read -r line; do
echo "Next line: $line"
done
# The loop reads from stdin because no redirection is specified in the while command itself.
# Alternatively, other commands can use stdin directly:
# tr 'a-z' 'A-Z' # This would convert all input to uppercase3. Using custom argument parsing (for
كود:
input_file=)
If you want to use the specific
كود:
input_file= format as in your example, you need to parse the arguments manually within your script.
[b]How to run the script:[/b]
bash
كود:
./your_script.sh input_file=/path/to/file.txt[b]Inside [/b]
كود:
your_script.sh[b]:[/b]
bash
كود:
#!/bin/bash
for arg in "$@"; do
case "$arg" in
input_file=*)
INPUT_FILE="${arg#*=}" # Extract the value after the '='
;;
*)
# Handle other arguments or errors
;;
esac
done
# Now you can use $INPUT_FILE as in Method 1
if [ -f "$INPUT_FILE" ]; then
echo "Processing file: $INPUT_FILE"
# ... processing code here ...
else
echo "Error: File not found or not specified."
fi
