Use echo to create a bash fils [closed] - linux

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.

Related

How replace string with regex in bash script? [closed]

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 10 days ago.
Improve this question
I have this working in terminal (it just remove the date using string replacement)
NAME="/home/me/a_path/2023-04-10 filename"
NEW_NAME=$(echo ${NAME//20[0-9][0-9]-[0-9][0-9]-[0-9][0-9] /})
echo ${NEW_NAME}
>>> Expected output : "/home/me/a_path/filename"
But this is not working (it output the non-modified string) in script, I can't understand why.
I tried different quotation marks positions and some other things that I found on SO, but nothing has worked for me yet.
I tried using sed it does not work better.
Edit: The example I gave is working, so probably a typo in my full script
Here is another approach without regex based on the inputs provided in the asked question.
#!/bin/bash
NAME="/home/me/a_path/2023-04-10 filename"
DIR=$(dirname "${NAME}")
FILE=$(basename "${NAME}" | awk '{print $NF}')
echo "${DIR}/${FILE}"
The output:
/home/me/a_path/filename

Look-back pwd until expression found on Linux [closed]

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 4 years ago.
Improve this question
Possible folder layouts:
path/to/a/project1/FOO-****/some/where/inside
path/to/another/project2/FOO-****/some/where/else/inside
I am looking for single line bash command that can drop me into a location FOO-**** which can be in any form, FOO-source, FOO-system-test or FOO-unit-test or any similarly named folder, while my pwd could be anywhere inside that.
What about something like this, searching backwards for a FOO* directory :
while [[ $PWD != / ]]; do [[ `basename "$PWD"` != "FOO"* ]] && cd .. || break ; done

Shell script to compare file names(without extension) in a directory [closed]

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

how do I basic script with linux? [closed]

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
what does this command do?
!/bin/bash
My first script
clear
echo (I don't know what will come after echo , can you help me with that too?)
./hello.shell
#!/bin/bash is called the shebang (you missed the leading #).
It tells which program will execute your script.
clear is for clearing screen.
echo outputs following argument to the standard output (your terminal by default). But you must not surround your string with parenthesis as it's used for grouping command in a sub-shell. If you want to print (...), you'll have too use double quotes :
echo "(I don't know what will come after echo , can you help me with that too?)"
./hello.shell will execute your script after you gave it execute permissions with chmod +x hello.shell.
Note that commonly used extension for a shell script is .sh rather than .shell.
For more, try theses links :
http://mywiki.wooledge.org/BashGuide/
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
#!/bin/bash tells to the SO that this file is a script and that bash is the shell that must execute it. So you can found: #!/opt/bin/perl for perl scripts, #!/bin/csh for c-shell, #!/bin/zsh ...

Bash script to read a file and run a command based on a specific string [closed]

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

Resources