I have a makefile where I want to read module name from input and then make directory based on it`s name. here is my code:
build:
#read -p "Enter Module Name:" module;
module_dir=./modules/$$module
mkdir -p $$module_dir/build;
But after setting module_dir, it contains only ./modules/ (with no module name concatenated).
What is wrong in my code?
thanks for your answers
Each command runs in its own subshell, so variables can't survive from one command to the next. Put them on the same line and they'll work:
build:
#read -p "Enter Module Name:" module; \
module_dir=./modules/$$module; \
mkdir -p $$module_dir/build
Related
I am trying to make a makefile that will ask the user for the name of the input and append this to create a $$(file).py but when I try and run this with "make do"
I get this error
do :
#echo "What is the name of the file?: "; \
read file; \
touch $$(file).py
Thank you!
Try this way:
.PHONY: do
do:
#echo "What is the name of the file?: "; \
read file && touch "$${file}.py"
.PHONY is to identify that "do" is not a file that make should look for, this will solve your error. [more info]
I made some changes too, by putting together the read and touch I removed weird outputs in between the 2 commands.
I am writing a bash script that will get the user defined filenames and then use alsa to play that file:
#!/bin/bash
export PATH=$PATH:/home/pi/Documents/audio
read -p "Enter a filename: " filename
aplay $filename
Above is what I have tried after reading about how to include path in a script. But at the terminal prompt, after I do ./script.sh, it returns no such a file in the directory. I also tried source ./script.sh thinking that the environment is only changed in the subshell and it returns the same.
All I need is to include the path within this script and does not change the system path permanently.
I'd appreciate it if someone can point me to the right direction. Thank you!
PATH is used for finding programs, not data files. If you want $filename to be a file in the audio directory, you need to concatenate it explicitly.
#!/bin/bash
audiodir=/home/pi/Documents/audio
read -r -p "Enter a filename: " filename
aplay "$audiodir/$filename"
Try specifying the absolute path of the file that you're playing like this:
#!/bin/bash
AUDIO_PATH="/home/pi/Documents/audio"
read -p "Enter a filename: " filename
aplay "${AUDIO_PATH}/${filename}"
In CLI command,
mkdir -p /data/bin
does the P flag stand for "path", since mkdir only creates an actual folder?
-p is not meant for path, is to create parent directory if necessary.
In fact, you can specify
-p or -parent
Create parent directories as necessary. When this option is specified, no error is reported if a directory already exists.
For example:
mkdir -p /example/directory/documents
Creates the directory /example/directory/documents. If example or directory do not exist, the they will be created using the -p option.
When a user inputs a name, there should be a new directory that gets created under that name.
In addition to that, the script needs to consult a file structure1.txt which is found in /etc/scriptbuilder/str1.
In this file, it will list two subdirectories (one on each line), the script is then supposed to create these two subdirectories in the new directory the user just made and named.
So how can the script then create each of the subdirectories that are listed in this text file?
I'm completely lost on that part.
This is my code so far:
echo "Enter the project name "
read name
echo $name
if [ ! -d $name ] then
mkdir $name
else
echo "The project name you entered already exists"
fi
cp /etc/scriptbuilder/str1/structure1.txt /$name
#I know this is wrong
because this would just copy the file over to the new directory but not actually
make the two subdirectories that are on the file onto the new directory
The bash command that you are looking for is read.
Also the syntax for your if [ ! -d "$name" ] should have a semicolon.
The else would typically have an exit 1 (or some such value).
Typical bash code gets input from the command line, but what you want is fine.
For testing purposes, I inserted a ~ (tilde), which references your home directory.
The script should look something like:
filename="/etc/scriptbuilder/str1"
read -p "Enter the project name " name
echo "$name"
if [ ! -d ~/"$name" ]; then
mkdir ~/"$name"
else
echo "The project name you entered already exists"
exit 1
fi
while read -r line; do
mkdir ~/"$name/$line"
done < "$filename"
You can clean up the formatting.
I am trying to write a bash script, where I want to read a path and a file with autocompletion. I used [read -e -p "path name:" pathname]. This works fine, but when I try [read -e -p $pathname filename], the file autocompletion just doesn't work. Can someone help me? Thanks in advance :)
The file autocompletion works from the current directory. If you want it to work in a given directory, you have to change the current directory to it:
read -e -p "path name:" pathname
cd $pathname
read -e -p $pathname filename