Converting linux command line to bash script?

Viewed 74

I'm trying to convert 2 command lines to work with bash but don't know how. And also make the bash script execute commands in the folder it's already in. This is about what it looks like right now. They work perfectly fine as command lines but not as bash commands.

#!/bin/bash

#Command that removes all files except the ones specified
cd folder1/folder2; rm -v !("file1"|"file2")

#Command that picks a random line from one file and makes another file with that line. But never the same one twice in a row.
grep -Fxv -f file1 /home/file2 | shuf -n 1 > tempfile; mv tempfile file1
1 Answers

Enable extglob

shopt -s extglob

Before the line of code that has:

!("file1"|"file2")

On a side note, you should add an exit after the cd just in case it failed for whatever reason.

cd folder1/folder2 || exit

Why? in your script the next command is rm after the cd so if cd failed the script will continue and rm will be executed on a directory that is not folder1/folder2

Related