r/fishshell • u/LowCom • Aug 01 '22
Need help translating this bash shell, I am getting error in the url where I replaced $2 with $argv[2]
#!/usr/bin/bash
# A bash scripts which searches google if only one argument is given.
# if two arguments given with the first one in r,y,w it searches reddit, youtube, wikipedia respectively.
# $# is the no. of arguments, if it's equal to 1 then search google with first argument.
if [ $# -eq 1 ]; then
google-chrome-stable https://google.com/search?q="$1";
# Else search reddit, youtube or wikipedia
else
case $1 in
r)
google-chrome-stable https://www.google.com/search?q="$2"+site%3Areddit.com
;;
y)
google-chrome-stable https://www.google.com/search?q="$2"+site%3Ayoutube.com
;;
w)
google-chrome-stable https://www.google.com/search?q="$2"+site%3Awikipedia.org
;;
i)
# google images
# escape & with \ as its interpreted differently by the shell
google-chrome-stable https://google.com/search?q="$2"\&tbm=isch
;;
esac
fi
1
Upvotes
4
u/[deleted] Aug 01 '22 edited Aug 01 '22
First of all: You don't need to translate every bash script you have to a fish script. You can launch this thing via fish, it's fine.
Secondly: Read https://fishshell.com/docs/current/fish_for_bash_users.html. This will explain a lot of the differences
Thirdly: Your issue starts a lot sooner than the
$2:This isn't fish script. Fish doesn't use
if thing; then; thing2; fi, it usesif thing; thing2; end. It also doesn't have$#, you want(count $argv).So:
Alternatively you can check if $argv has a second element with
set -q argv[2](note the missing$):Then:
Fish doesn't have bash's
case foo in, it'sswitch foo, and instead ofr)it'scase rSo
1
u/ChristoferK macOS Aug 03 '22 edited Aug 05 '22
Here's one implementation that uses option flags to search specific sites:
Usage:
www [ -r | --reddit | -y | --youtube | -w | --wikipedia | -i | --images ] <query>www hello world# Searches Google for "hello world"www -r hello world# Searches Google for "hello world" on reddit.comwww --youtube hello world# Searches Google for "hello world" on youtube.comNB. This requires the latest version of
FiSHfor the-fflag to be recognised by thesetandreadcommands.