Linux: Get a Script to Be Able to Ask The User for a File Name Then Open That File

linux: get a script to be able to ask the user for a file name then open that file

First of all, you have an error there:

hello "welcome"

That doesn't do anything unless you have a command on your system called hello. To print a message, use

echo "welcome"

To get input from the user after printing a message, use read. Since you're using bash, you can use the -p option to present a message and save user input with one command:

read -p message" variable

To find and display the contents of the file, you can use the find command and its -exec option. For example, -exec less to display the file using less.

Then, you have various other errors. A working version of your script would be something like:

#!/usr/bin/env bash

echo 'Welcome!'

while [ "$response" != "n" ]
do
read -p "Would you like to find a file? [y/n]:" response
case $response in
y) read -p "What is the name of the file? " file
find . -type f -name "$file" -exec less {} \;
;;

n)
echo "goodbye"
exit ;;
esac
done

Unix shell to prompt user for filename

That works fine, at least in bash. Well, the read works fine. However, the assignment to result should probably be:

result=$(wc -l $filename)

but, since that command outputs both the line count and the filename, you might want to change it a little to just get the line count, something like:

result=$(cat $filename | wc -l)

or:

result=$(wc -l <$filename)

The command you have:

result=wc -l $filename

will set result to the literal wc, then try to execute the -l command.

For example, the following five-line script:

#!/bin/bash
echo "Pls enter your filename:"
read filename
result=$(cat $filename | wc -l)
echo "Your file has $result lines"

will, when run and given its name as input, produce the following:

Your file has 5 lines

If you're not using bash, you need to specify which shell you are using. Different shells have different ways of doing things.

Take a file name as input and check if it exists

First of all, I want to thank anyone and everyone who tried to help. After 3 hard working days, I found the answer, here it is:

#!/bin/bash

file="$@"
if [ -f $file ]
then
echo "File exists"
else
echo "File does not exist"
fi

Using this table:



















































Variable NameDescription
$0The name of the Bash script
$1 - $9The first 9 arguments to the Bash script
$#Number of arguments passed to the Bash script
$@All arguments passed to the Bash script
$?The exit status of the most recently run process
$$The process ID of the current script
$USERThe username of the user running the script
$HOSTNAMEThe hostname of the machine
$RANDOMA random number
$LINENOThe current line number in the script

How do I know the script file name in a Bash script?

me=`basename "$0"`

For reading through a symlink1, which is usually not what you want (you usually don't want to confuse the user this way), try:

me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"

IMO, that'll produce confusing output. "I ran foo.sh, but it's saying I'm running bar.sh!? Must be a bug!" Besides, one of the purposes of having differently-named symlinks is to provide different functionality based on the name it's called as (think gzip and gunzip on some platforms).


1 That is, to resolve symlinks such that when the user executes foo.sh which is actually a symlink to bar.sh, you wish to use the resolved name bar.sh rather than foo.sh.

Open and write data to text file using Bash?

The short answer:

echo "some data for the file" >> fileName

However, echo doesn't deal with end of line characters (EOFs) in an ideal way. So, if you're going to append more than one line, do it with printf:

printf "some data for the file\nAnd a new line" >> fileName

The >> and > operators are very useful for redirecting output of commands, they work with multiple other bash commands.

Create new file but add number if filename already exists in bash

The following script can help you. You should not be running several copies of the script at the same time to avoid race condition.

name=somefile
if [[ -e $name.ext || -L $name.ext ]] ; then
i=0
while [[ -e $name-$i.ext || -L $name-$i.ext ]] ; do
let i++
done
name=$name-$i
fi
touch -- "$name".ext

How to cat EOF a file containing code?

You only need a minimal change; single-quote the here-document delimiter after <<.

cat <<'EOF' >> brightup.sh

or equivalently backslash-escape it:

cat <<\EOF >>brightup.sh

Without quoting, the here document will undergo variable substitution, backticks will be evaluated, etc, like you discovered.

If you need to expand some, but not all, values, you need to individually escape the ones you want to prevent.

cat <<EOF >>brightup.sh
#!/bin/sh
# Created on $(date # : <<-- this will be evaluated before cat;)
echo "\$HOME will not be evaluated because it is backslash-escaped"
EOF

will produce

#!/bin/sh
# Created on Fri Feb 16 11:00:18 UTC 2018
echo "$HOME will not be evaluated because it is backslash-escaped"

As suggested by @fedorqui, here is the relevant section from man bash:

Here Documents

This type of redirection instructs the shell to read input from the
current source until a line containing only delimiter (with no
trailing blanks) is seen. All of the lines read up to that point are
then used as the standard input for a command.

The format of here-documents is:

      <<[-]word
here-document
delimiter

No parameter expansion, command substitution, arithmetic expansion,
or pathname expansion is performed on word. If any characters in word
are quoted, the delimiter is the result of quote removal on word, and
the lines in the here-document are not expanded. If word is
unquoted, all lines of the here-document are subjected to parameter
expansion, command substitution, and arithmetic expansion
. In the
latter case, the character sequence \<newline> is ignored, and \
must be used to quote the characters \, $, and `.

Extract filename and extension in Bash

First, get file name without the path:

filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"

Alternatively, you can focus on the last '/' of the path instead of the '.' which should work even if you have unpredictable file extensions:

filename="${fullfile##*/}"

You may want to check the documentation :

  • On the web at section "3.5.3 Shell Parameter Expansion"
  • In the bash manpage at section called "Parameter Expansion"

How to get a filename in a bash script without the keyboard

You should be able to create your own 'program' in a bash script which takes its first argument to be the filename using the convention "$1".

The bash script should look something like the below. I tested it, storing the script in the file cachedvlc.sh. The inverted commas helping to handle whitespace and weird characters...

#!/bin/bash
cat "$1" > /dev/null
vlc "$1"

...and will need to be made executable by changing its permissions through the file manager or running this in the terminal...

chmod u+x cachedvlc.sh

Then within your operating system, associate your bash script with the type of file you want to launch. For example on Ubuntu, you could add your script and call it 'Cached VLC' to the Menu using the 'Main Menu' application, then right-click on the file in Nautilus and choose 'Open with' to select your bash script.

After this, double-clicking or right-clicking on a file within your filemanager should be good enough to launch a cached view. This assumes what you say about caching is in fact correct, which I can't easily check.



Related Topics



Leave a reply



Submit