shell prompt not showing up after running a script - linux

as per http://linuxcommand.org/lc3_wss0150.php i am trying to run this script
#!/bin/bash
# Program to print a text file with headers and footers
TEMP_FILE=./printfile.txt
pr $1 > $TEMP_FILE
echo -n "Print file? [y/n]: "
read
if [ "$REPLY" = "y" ]; then
less $TEMP_FILE
fi
but when i run it via
./print_demo.bash
which is what it is saved as in my bin directory, it does not echo "Print file? [y/n]:" and also does not return the shell prompt. i have to ctrl^c to get it back.

That script is expecting input.
pr "$1" > $TEMP_FILE
The $1 represents the first argument from the command line
./print_demo.bash <printable_filename_here.txt>

Related

Using ls command result in a loop

I want to use the result of ls command in a loop to check if for example the first line is a directory, second etc.
For example I have this folder that contains one directory the script should display:
18_05_2018 is directory
enter image description here
Create a file named is_file_or_directory.sh containing:
cd "$1" || echo "Please specify a path" && exit
for i in *; do
if [[ -d $i ]]; then
echo "$i is a directory"
elif [[ -f $i ]]; then
echo "$i is a file"
else
echo "$i is not valid"
exit 1
fi
done
Make that file executable with:
sudo chmod +x is_file_or_directory.sh
Run the script specifying as a parameter the path that you want to analyze:
./is_file_or_directory.sh /root/scripts/
Output:
jeeves ~/scripts/stack # ./is_file_or_dir.sh /root/scripts/
databe.py is a file
is_file_or_dir.sh is a file
mysql_flask.py is a file
test is a directory
Here's a more detailed explanation of what is happening under the hood. The variable $1 is, in Bash, the first parameter sent to the script. In our case it is the path where the script will perform its actions. Then we use the variable $i in the loop.
$i content will be every file / folder name in the path $1. With -d and -f we check if $i is a file or a folder.

Run commands from a text file through a bash script

I am attempting to write a script that will read through a text file, and then execute every line that begins with the word "run" or "chk" as a command. This is what I have thus far:
#!/bin/bash
counter=1
for i in $#
do
while read -r line
do
if [[ ${line:0:4} == "run " ]]
then
echo "Now running line $counter"
${line:4:${#line}}
elif [[ ${line:0:4} == "chk " ]]
then
echo "Now checking line $counter"
${line:4:${#line}}
elif [[ ${line:0:2} == "# " ]]
then
echo "Line $counter is a comment"
else
echo "Line $counter: '$line' is an invalid line"
fi
counter=$((counter+1))
done<$i
done
However, when I feed it a text file with, for example the commands
run echo > temp.txt
It does not actually create a file called temp.txt, it just echoes "> temp.txt" back to the stdout. It also does a similar thing when I attempt to do something like
run program arguments > filename.txt
It does not put the output of the program in a file as I want, but it rather tries to treat the '>' as a file name.
I know this is a super specific and probably obvious thing, but I am very new to bash and all shell scripting.
Thanks
You need to use eval to do all the normal shell parsing of the variable:
eval "${line:4}"
You also don't need :${#line}. If you leave out the length, it defaults to the rest of the string.

Desktop file not executing command

I have this simple command to check if a file exists:
if [ -f /tmp/file.txt ] ; then echo "yes" ; else echo "no" ; fi
If I run it direcly on terminal, it works (shows "yes" if the file exists and "no" if it doesn't). But I want to execute this command inside a .desktop file using it as a value to Exec key:
[Desktop Entry]
Version=1.0
Type=Application
Exec=if [ -f /tmp/file.txt ] ; then echo "yes" ; else echo "no" ; fi
StartupNotify=true
Terminal=false
Categories=Utility;X-XFCE;X-Xfce-Toplevel;
MimeType=x-scheme-handler/custom
Name=Custom Test
Comment=Custom
If I try to execute xdg-open custom:// I get custom://: error opening location: The specified location is not supported, but if I change Exec value to echo "yes" and execute xdg-open custom://, it shows yes on terminal.
What am I missing here?
You are trying to execute shell script coding in .desktop file which is not supported.
The reason why "echo yes" worked is .desktop executes the echo command with paramter as "yes" which is acceptable.
.desktop executes commands along with options and parameters. You can write the shell script code in a .sh file and mentioned it in Exec or Run the code using
Exec=sh -c "if [ -f /tmp/file.txt ] ; then echo 'yes' ; else echo 'no' ; fi"
Here .desktop executes "sh" with options and params
Try setting your Exec to:
bash -c 'if [ -f /tmp/file.txt ] ; then echo "yes" ; else echo "no" ; fi'
The 'if' command is a bash-builtin, not an external command.

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.

Resources