Okay, I have an extremely simple shell script here. It uses the string operators ## and % to separate the directory name from the filename. I'm trying to build a larger script that replaces text in files, but I can't get this part to work:
When I type source files hello/goodbye I get:Code:#!/bin/bash #script: files dire=${1%/*}; fil=${1##*/}; echo "Directory: $dire"; echo "File: $fil"; unset dir; unset fil;
Directory: hello
File: goodbye
When I type source files ~/Desktop/hello I get:
Directory: /Users/chuckboswick/Desktop
File: hello
When I type source files ~/Desktop/* I get:
Directory: /Users/chuckboswick/Desktop
File: 1226250839986du7.gif
The problem appears to be that the shell script interpreter is interpreting the string as a file path immediately after I type it. I don't want it to do that. I want it to treat it as just a plain, simple string and not recognize it as a file path. I want to eventually get a list of files by typing for file in $(ls $fil) once I'm inside the directory. How can I protect the string $1 from being immediately interpreted, but still be able to use it to list files later?
Life's too short to be cool. Be a nerd.
The beauty of shell scripting is 5000 commands to choose from.
If I understand you correctly...
Consider the following:
basename to extract the filename
dirname to extract the directory path
and sed or awk to do your text replacement
Everything including the loop will probably be 15 lines or less.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks