How to autocomplete path and file in a bash script? - bash

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

Related

how to include path of a directory containing files inside bash script

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}"

Error: Tar command not found

echo "Enter path of backup file e.g /tmp/backup/etc.tar.gz : "
read PATH #input was /tmp/backup/etc.tar.gz
echo "Enter directory: "
read DIR #input was /root/testing
sudo tar -zvxf "$PATH" -C "$DIR"
when I ran the script, it said that the command was not found. I tried using whatever kind of brackets for the variables but still not working. Any help?
However when I ran the command tar -zvxf /tmp/backup/etc.tar.gz -C /root/testing , it worked.
You're saving something into PATH which is what the shell will search to find the executables. So when you use that variable the shell can't find, say, tar because it is no longer in your search path. Use a different variable name.

Bash: passing a variable to mv command option

--Bash 4.1.17 (running with Cygwin)
Hello, I am trying to pass the date into the --suffix option on the move (mv) command. I am able to pass in a simple string (like my name) but unable to pass in the date. If you run the script below you will see that the mv command with the suffix="$var" works but suffix="$now" does not.
#!/bin/bash
dir="your directory goes here"
now="$(date "+%m/%d/%y")"
var="_CARL!!!"
echo "$now"
echo "$var"
cd "$dir"
touch test.txt
# error if already exists
mkdir ./stack_question
touch ./stack_question/test.txt
mv -b --suffix="$var" test.txt ./stack_question/
The idea is that if test.txt already exists when trying to move the file, the file will have a suffix appended to it. So if you run this script with:
--suffix="$var"
you will see that the stack_question directory contains two files:
test.txt & test.txt_CARL!!!
But, if you run this script with:
--suffix="$now"
you will see that in the stack_question directory only contains:
test.txt
Any help on this would be greatly appreciated!
It is because you have embedded / in your date format try
now="$(date +%m_%d_%y)"

simple cp $file1 $file2 script not working?

I tried searching for this question but I couldn't find a thread specifically like this one. I am trying to do a practice script where you enter in a source and a destination and the program copies the files for you.
./testscript
"name of file to copy?" file1
"to be copied to?" file2
"file1 has been copied to file2"
So far I have this:
#!bin/bash
echo -e 'Enter file name of file you wish to copy'
read $FILE1
echo -e 'Enter the file you wish to copy to'
read $FILE2
if cp $FILE1 $FILE2
then echo -e 'Copy was successful'
else echo -e 'Copy was unsuccessful'
fi
The program error is saying bad interpreter. I don't really understand, it looks okay from my end.
The path you supplied as interpreter is not absolute (missing '/').
I suggest changing #!bin/bash to #!/usr/bin/env bash
.
See The difference between "#! /usr/bin/env bash" and "#! /usr/bin/bash"? for details.
You have provided incorrect path to the interpreter. You are probably missing a forward slash in the beginning.
The shebang statement should say: #!/bin/bash (or whatever the correct path is)

Prompt for file path: home directory alias and case-sensitive tab completion

I am writing a shell script that prompts the user for a file path:
read -e -p "Enter the path to the file: " FILEPATH
I am then using this file path to perform operations – namely to compress a folder.
(cd "$FILEPATH"; tar -cvz *) > /tmp/torrent.tar.gz;
At the prompt, if I use the ~ alias (home directory), then the shell script doesn't seem to understand this, as the tar function compresses the wrong path. Is there anyway I can allow for this alias?
Also, tab completion seems to be case-sensitive at the prompt. I was wondering how I can change that?
Example using eval:
read -e -p "Enter the path to the file: " FILEPATH
eval FILEPATH=$FILEPATH
cd $FILEPATH
echo $PWD
In your case it becomes:
read -e -p "Enter the path to the file: " FILEPATH
eval FILEPATH=$FILEPATH
(cd "$FILEPATH"; tar -cvz *) > /tmp/torrent.tar.gz;
To deal with spaces you can use sed:
read -e -p "Enter the path to the file: " FILEPATH
FILEPATH=$(echo $FILEPATH | sed 's/ /\\ /')
eval FILEPATH=$FILEPATH
cd "$FILEPATH"
echo $PWD
You could apply the substitution yourself like this:
filepath=${filepath/\~/$HOME}
I don't know whether there's a way to get the shell to do it for you.
Here's an answer to your other question: https://superuser.com/questions/90196/case-insensitive-tab-completion-in-bash

Resources