Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am very new to scripting and would like to know how to write a script fileCreate which has two optional parameters two input arguments as shown below:
fileCreate <filename> <path>
<filename> parameter signifies the name of the file in which the
contents will be saved
<path> parameter signifies the directory structure where the file will
be saved.
When the program is executed it will open an editor and user can input any contents, that will be saved as specified.
If no parameters are passed, the default is current directory and userfileX (userfileX to the number of times the file is created)
If there already exists a file of similar name and having same contents then the user should be able to append the file or else just the date gets modified.
Using vi as the editor:
D=${2:-.}
F=$1
if [ -z "$F" ] ; then
X=1
F="userfile$X"
while [ -f "$F" ] ; do
X=$(($X+1))
F="userfile$X"
done
fi
vi "$D/$F"
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I wanted to make a one-liner that would create an executable. It's for a quick guide I' making for a friend, so that it would be an easy copy-paste job.
I have this:
export FILE=spotify; [ -f ~/.local/bin/$FILE ] && echo "File $FILE already exists. Couldn't create it..."; [ -f ~/.local/bin/$FILE ] || { echo $"#!/bin/sh\nflatpak run com.spotify.Client" > ~/.local/bin/$FILE; chmod +x ~/.local/bin/$FILE; echo "Successfully created $FILE" }
However I am stuck on echo "#!/bash/sh" which causes "event not found"...
Any ideas how to overcome that?
In a Bash interactive shell the exclamation mark is used for searching the Bash history. Specifically, it replaces the string after the exclamation mark with the first entry in the history which matches that string. If there is no such entry, you get the symptom you're seeing.
The solution is to use a single-quoted string.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a source code which is having text and binary file. I have to find and collect all the human unreadable files present in source code. How I can do this?
Although the answer of Far Had is correct, you don't even need a for-loop for this. As you state yourself, all your files are within one directory, so you can simply run:
file *
The answers containing "text" (be it ASCII, unicode or something else) indicate human readable files.
This piece of code returns a list of all non ascii text files in current directory.
Hope this will help:
for i in `find . -type f`; do file $i; done |grep -v text | cut -d : -f 1
You could replace the . (dot) after the find with any other location in your filsystem.
One way is to use perl (File::Find module) like this:
perl -MFile::Find -e '#directories=shift || "."; sub wanted { ! -T && print "$File::Find::name\n"; }; find(\&wanted, #directories);'
NOTE: The above command defaults to searching the current directory.
To search a specific directory e.g. /tmp, just type the above command followed by a space and /tmp
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My requirement is I will have xml and pdf files like pairs.(e.g.,file1.xml, file1.pdf and file2.xml,file2.pdf) in same folder.
I need to check for xml files which are not having pdf pair and move them to different folder.(e.g., if file3.xml doesn't have file3.pdf, I need to move it to different folder).
Please answer me the shell script to do get this functionality done.
You can remove the extension using parameter expansion:
#! /bin/bash
for file in *.xml ; do
if [[ ! -f ${file%.xml}.pdf ]] ; then
mv "$file" folder/
fi
done
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm in of need of help.
How do i write a shell script program using Apache Tika as the converter to help me execute .pdf files in one directory and save them in another directory in a different format (json, xml, etc).
A rough example, using JSON:
outdir=../out.d
mkdir -p "$outdir"
for f in *.pdf; do
java -jar tika-app.jar --json "$f" >"$outdir/${f%.pdf}.json" \
|| rm -- "$outdir/${f%.pdf}.json"
done
Obviously, this expects tika-app.jar to be in the current directory; adjust to taste. Similarly, see http://tika.apache.org/1.6/gettingstarted.html for full command-line usage.
To understand the ${f%.pdf} shell syntax, see BashFAQ #73.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am needing a bash script which, when a file is saved to a particular directory (file name could be anything), read its contents and then, if a particular string in the file is found then run a specific command/or another script.
I have had a look through Bash script: perform actions based on file contents but this script appears to depend on the file being named. My bash scripting is next to useless so hoping someone can help :)
If you install the inotify-tools package, you can use inotifywait for this:
#!/bin/bash
DIR_TO_WATCH=/tmp
STRING=foobar
cd "$DIR_TO_WATCH"
inotifywait -qme close_write --format '%f' -r ./ | while read changed_file; do
if grep "$STRING" "$changed_file" &>/dev/null; then
echo "$STRING found on file $changed_file!"
fi
done
I recommend you take a look at inotifywaits manual for more details on the command-line options