How can I pass the output of the cat/ls -l command to an argument in unix? - linux

I've got an assignment that says, "If you enter 'type', it should respond with the prompt 'Enter file name:' and then have you type something in. If you enter a valid file name, it should respond with a listing of the file, and exit."
Here's the code I have so far:
type)
echo "Enter file name:"
read var
if [ -f "${var}" ];
then
???
exit
fi
;;
I'm trying to figure out what to enter where I put the ??? to display the contents of whatever file is entered the same way the cat command would. I thought it'd be something simple like echo $CAT but that hasn't worked.
And then I'm trying to do the same thing but with a directory, outputting the ls -l listing of it rather than file contents.
Help would be appreciated!

You can do something like this:
if [ -f "$var" ]
then
cat "$var"
elif [ -d "$var" ]
then
ls -l "$var"
fi

Related

Is there a way to pipe user input within a bash script into the cat command and have it save at a destination of my choosing as a text file

Something similar to this maybe:
#! /bin/bash
echo What is your name?
read name | cat > ~/Documents/file.txt
if [[ $name==Bob ]]
echo something
fi
The command creates an empty file on manjaro mint.
Your problem is that read doesn't create any output.
And you have a syntax error further down the line, it would be a good idea to put your script(s) through shellcheck.
#! /bin/bash
echo What is your name?
read -r name
echo "$name" > ~/Documents/file.txt
if [[ "$name" == "Bob" ]]; then
echo something
fi

Copy multiple files with bash script from command line arguments?

I want to create a script that allows me to enter multiple filenames from the command line, and have the script copy those files to another directory. This is what I am trying but I keep getting an error of
line 10: binary operator expected
#!/bin/bash
DIRECTORY=/.test_files
FILE=$*
if [ -e $DIRECTORY/$FILE ]; then
echo "File already exists"
else
cp $FILE $DIRECTORY
fi
So if the script was named copfiles.sh, I am writing...
./copyfiles.sh doc1.txt doc2.txt
It will move the files, but if they already exist, it won't read the error message.
Also I get the "line 10: binary operator expected" error regardless of it the files are there or not. Can anyone tell me what I am doing wrong?
As a possible problem, if you had a filename with a space or had multiple arguments $* would have spaces in it so [ -e $DIR/$FILE ] will expand to have lots of words, like [ -e /.test_files/First word and more ] and -e expects just 1 word after it. Try putting it in quotes like
if [ -e "$DIRECTORY/$FILE" ]
Of course, you may only want to store $1 in $FILE to get just the first argument.
To test all the arguments you want to loop over the arguments and test each with something like
for FILE in "$#"; do
if [ -e "$DIRECTORY/$FILE" ]; then
echo "$FILE already exists"
else
cp "$FILE" $DIRECTORY
fi
done
Using quotes around $# to preserve spaces in the original arguments as well

Create parameters for command Linux shell script

Hello I am trying to create parameters for my shell script but I am having trouble.
lets say for example the file is called test.
When I call ./test -parameter1 input_file.txt
I get an error saying 'no such file or directory'.
Here is an example of my code.
#!/bin/sh
if [ "$1" == "parameter" ]
then
while read line
do
#echo "$line"
done <$1
else
echo "Not working"
fi
My over all goal of this is to read in a file of numbers line by line which I have working, then to calculate the average values of the rows or by columns. Which is why I am trying to create parameters so the user will have to specify ./test -rows input_file.txt or ./test -columns input_file.txt
You are using the string -parameter as the input file name. Perhaps you want:
#!/bin/sh
if [ "$1" = "-parameter" ]
then
while read line
do
#echo "$line"
done <$2 # Use $2 instead of $1 here. Or use shift
else
echo "Not working" >&2
fi

How to create a shell script that can scan a file for a specific word?

one of the questions that I have been given to do for my Computer Science GCSE was:
Write a shell script that takes a string input from a user, asks for a file name and reports whether that string is present in the file.
However way I try to do it, I cannot create a shell script.
I don't need you to tell me the whole number, however, I have no idea where to start. I input the variable and the file name, however, I have no idea how to search for the chosen word in the chosen file. Any ideas?
Using grep can get this working, for example
viewEntry()
{
echo "Entering view entry"
echo -n "Enter Name: "
read input
if grep -q "$input" datafile
then
echo ""
echo -n "Information -> "
grep -w "$input" datafile
echo ""
else
echo "/!\Name Not Found/!\\"
fi
echo "Exiting view entry"
echo ""
}
dataFile is the file you would be reading from. Then making use of -q and -w arguments of grep, you should be able to navigate your chosen file.
This site does a great job explaining grep and your exact problem: http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
The following shell-script is a very quick approach to do what you suggested:
#!/bin/sh # Tell your shell with what program this script should be exectued
echo "Please enter the filename: "
read filename # read user input into variable filename
count=`grep -c $1 $filename` # store result of grep into variable count
if [ $count -gt 0 ] # check if count is greater than 0
then
echo "String is present:" $1
else
echo "String not found:" $1
fi
You should look at some tutorials to get the basics of shell-scripting. Your task isn't very complex, so after some reading you should be able understand what the script does and modify it according your needs.

How to open an editor from a bash function?

I have a simple function to open an editor:
open_an_editor()
{
nano "$1"
}
If called like open_an_editor file.ext, it works. But if I need to get some output from the function — smth=$(open_an_editor file.ext) — I cannot see the editor, script just stucks. What am I missing here?
Update: I am trying to write a function which would ask the user to write a value in editor, if it wasn't given in script arguments.
#!/bin/bash
open_an_editor()
{
if [ "$1" ]
then
echo "$1"
return 0
fi
tmpf=$(mktemp -t pref)
echo "default value, please edit" > "$tmpf"
# and here the editor should show up,
# allowing user to edit the value and save it
# this will stuck without showing the editor:
#nano "$tmpf"
# but this, with the help of Kimvais, works perfectly:
nano "$tmpf" 3>&1 1>&2 2>&3
cat "$tmpf"
rm "$tmpf"
}
something=$(open_an_editor "$1")
# and then I can do something useful with that value,
# for example count chars in it
echo -n "$something" | wc -c
So, if the script was called with an argument ./script.sh "A value", the function would just use that and immediately echo 7 bytes. But if called without arguments ./script.sh — nano should pop up.
If the input you need is the edited file, then you obviously need to cat filename after you do the open_an_editor filename
If you actually need the output of the editor, then you need to swap stderr and stdin i.e:
nano "$1" 3>&1 1>&2 2>&3
If yo need 'friendly' user input, see this question on how to use whiptail
if you need to get output from function and store in variable, you just display what's in file.
open_an_editor()
{
cat "$1"
}
smth=$(open_an_editor file.txt)
If all you want is for a user to enter a value then read is enough:
OLDIFS="$IFS"
IFS=$'\n'
read -p "Enter a value: " -e somevar
IFS="$OLDIFS"
echo "$somevar"

Resources